
Problem
Solution
Submissions
Word Break Problem
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Implement a function `wordBreak(s, wordDict)` that determines if a string `s` can be segmented into a space-separated sequence of one or more dictionary words. The same word in the dictionary may be reused multiple times in the segmentation. Return `True` if possible, otherwise `False`.
Example 1
- Input: s = "Tutorialspoint" wordDict = ["Tutorials","point"]
- Output: True
- Explanation:
- Step 1: Take the input string "Tutorialspoint".
- Step 2: Check if it can be segmented using words from wordDict.
- Step 3: The string can be divided into "Tutorials" and "point".
- Step 4: Both "Tutorials" and "point" are in wordDict.
- Step 5: Therefore, return True as "Tutorialspoint" can be segmented as"Tutorials point".
Example 2
- Input: s = "applepenapple" wordDict = ["apple", "pen"]
- Output: True
- Explanation:
- Step 1: Take the input string "applepenapple".
- Step 2: Check if it can be segmented using words from wordDict.
- Step 3: The string can be divided into "apple", "pen", and "apple".
- Step 4: All these segments are in wordDict.
- Step 5: Note that the same word "apple" is used twice in the segmentation.
- Step 6: Therefore, return True as "applepenapple" can be segmented as "apple pen apple".
Constraints
- 1 ≤ s.length ≤ 300
- 1 ≤ wordDict.length ≤ 1000
- 1 ≤ wordDict[i].length ≤ 20
- s and wordDict[i] consist of only lowercase English letters
- All the strings of wordDict are unique
- Time Complexity: O(n²) where n is the length of string s
- Space Complexity: O(n)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Consider using dynamic programming to avoid redundant calculations
- Use a boolean array dp[i] to indicate whether s[0...i-1] can be segmented
- For each position i, check if any previous position j makes dp[j] true and s[j...i-1] is in the wordDict
- Memoization can significantly improve performance for recursive approaches
- Consider using a trie data structure for efficient word lookup