Given a string s
and a dictionary of strings word_dict
, return True
if s
can be segmented into a space-separated sequence of one or more dictionary words.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Input: s = "leetcode"
, word_dict = ["leet", "code"]
Output: True
Explanation:
We return True
because "leetcode" can be segmented as "leet code".
Input: s = "applepenapple"
, word_dict = ["apple", "pen"]
Output: True
Explanation:
We return True
because "applepenapple" can be segmented as "apple pen apple". Note that "apple" can be used twice.
Input: s = "catsandog"
, `word_dict = ["cats", "dog", "sand", "and", "cat"]``
Output: False
Explanation:
We return False
because "catsandog" cannot be segmented with the words provided in the dictionary without overlap.
This exercise was taken from: Leetcode: Word Break