LeetCode 139 - Word Break
LeetCode 139 - Word Break
Table of Contents
Problem Statement
Given a string s
and a dictionary of strings wordDict
, 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.
Using Dynamic Programming
We use dynamic programming (DP) to determine whether the string s
can be segmented into valid dictionary words.
Dictionary Lookup:
- To speed up checking if a word exists in
wordDict
, we use an unordered set (dict
) for 𝑂(1) lookups.
- To speed up checking if a word exists in
Dynamic Programming:
- The outer loop iterates over the length of
s
(i
), and the inner loop iterates over potential split points (j
) in the substrings[0:i]
.
- The outer loop iterates over the length of
Efficient Substring Matching:
- For each
i
andj
, check ifs[j:i]
is inwordDict
and ifdp[j]
istrue
.
- For each
Final Result:
- If
dp[n]
istrue
, the strings
can be segmented; otherwise, the string cannot.
- If
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> dict(wordDict.begin(), wordDict.end());
int n = s.size();
vector<bool> dp(n + 1, false);
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && dict.count(s.substr(j, i - j))) {
dp[i] = true;
break;
}
}
}
return dp[n];
}
};
Complexity
- Time Complexity:
𝑂(𝑛2+𝑚), where 𝑛 is the length of
s
and 𝑚 is the total number of characters inwordDict
(building the set).The nested loops result in 𝑂(𝑛2), and substring lookups and dictionary checks are 𝑂(1) due to the hash set.
- Space Complexity:
- 𝑂(𝑛+𝑚), for the dp array and the dictionary set.
Conclusion
This method efficiently ensures all possible segmentations are checked systematically.