forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathword-break.cpp
More file actions
27 lines (23 loc) · 726 Bytes
/
word-break.cpp
File metadata and controls
27 lines (23 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Time: O(n * l^2), l is the max length of the words.
// Space: O(n)
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
const int n = s.length();
size_t max_len = 0;
for (const auto& str: wordDict) {
max_len = max(max_len, str.length());
}
vector<bool> canBreak(n + 1, false);
canBreak[0] = true;
for (int i = 1; i <= n; ++i) {
for (int l = 1; l <= max_len && i - l >= 0; ++l) {
if (canBreak[i - l] && wordDict.count(s.substr(i - l, l))) {
canBreak[i] = true;
break;
}
}
}
return canBreak[n];
}
};