#C10387. String Segmentation with Dictionary
String Segmentation with Dictionary
String Segmentation with Dictionary
Given a string s
and a dictionary of words, determine whether the string can be segmented into a sequence of one or more dictionary words.
For example, if s = "leetcode"
and the dictionary is ["leet", "code", "apple", "pen"]
, then the answer is YES
since s
can be segmented as leet code
.
Use a dynamic programming approach to solve this problem efficiently. The key idea is to check all possible partitions of the string and test if the left part is segmentable while the right part exists in the dictionary.
inputFormat
The input is read from standard input (stdin). The first line contains the string s
. The second line contains space-separated words representing the dictionary.
outputFormat
Output a single line to standard output (stdout): YES
if the string can be segmented into one or more dictionary words, or NO
otherwise.## sample
leetcode
leet code apple pen
YES