Practice Problem
Word Break
Difficulty: Medium
Given a string and a dictionary of words, determine if the string can be segmented into a space-separated sequence of dictionary words.
Word Break
Given a string s and a list of strings wordDict representing a dictionary, return true if s can be segmented into a space-separated sequence of one or more dictionary words.
Note: The same word in the dictionary may be reused multiple times in the segmentation.
Examples
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: "leetcode" can be segmented as "leet code".Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: "applepenapple" can be segmented as "apple pen apple".
Note that "apple" is reused.Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
Explanation: No valid segmentation exists for "catsandog".Constraints
1 <= s.length <= 3001 <= wordDict.length <= 10001 <= wordDict[i].length <= 20sandwordDict[i]consist of only lowercase English letters- All strings in
wordDictare unique
Expected Complexity
- Time: O(n^2 * m) where n is the length of s and m is the average word length (for substring comparison), or O(n^2) with hashing
- Space: O(n + k) where k is the total size of the dictionary
MEDIUM
Dynamic Programming
Tabulation
Hash Map / Dictionary
Set
Strings
Algorithms
Intermediate
0 views
Solution
Hints
Hint 1
Hint 2
Premium
Hint 3
Premium
Hint 4
Premium
This section is available for CodeSnatch Premium members only.
