-
Notifications
You must be signed in to change notification settings - Fork 0
/
140-Word Break II.cpp
31 lines (29 loc) · 983 Bytes
/
140-Word Break II.cpp
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
28
29
30
31
class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
vector<string> res;
unordered_map<int, vector<string>> record;
return dfs(record, 0, s, wordDict);
}
vector<string> dfs(unordered_map<int, vector<string>>& record, int pos,
string& s, vector<string>& wordDict){
int n = s.size();
if(record.count(pos)) return record[pos];
vector<string> res;
for(auto& word : wordDict){
int m = word.size();
if(pos + m <= n && s.substr(pos, m) == word){
if(pos + m == n) {
res.push_back(word);
continue;
}
vector<string> tmp = dfs(record, pos + m, s, wordDict);
for(auto str : tmp){
res.push_back(word + " " + str);
}
}
}
record[pos] = res;
return res;
}
};