-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path140.cpp
More file actions
30 lines (27 loc) · 980 Bytes
/
Copy path140.cpp
File metadata and controls
30 lines (27 loc) · 980 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
28
29
30
class Solution {
unordered_map<string, vector<string>> dp;
vector<string> append(vector<string>& ans, string& right) {
vector<string> res;
for (string& str : ans) res.push_back(str + " " + right);
return res;
}
vector<string>& wordBreak(string s, unordered_set<string>& wd) {
if (dp.count(s)) return dp[s];
vector<string> ans;
if (wd.count(s)) ans.push_back(s);
for (int i = 1; i < s.size(); i++) {
string right = s.substr(i);
if (wd.count(right) == 0) continue;
string left = s.substr(0, i);
vector<string> res = append(wordBreak(left, wd), right);
ans.insert(ans.end(), res.begin(), res.end());
}
dp[s].swap(ans);
return dp[s];
}
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> wd(wordDict.begin(), wordDict.end());
return wordBreak(s, wd);
}
};