-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path140.cpp
More file actions
30 lines (28 loc) · 906 Bytes
/
140.cpp
File metadata and controls
30 lines (28 loc) · 906 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 {
public:
vector<string> res;
void backtracking(string& s, unordered_set<string>& st, int curr, vector<string>& path) {
if (curr == s.size()) {
string out;
for (auto word : path) out += word + " ";
out.pop_back();
res.push_back(out);
return;
}
for (int i = curr + 1; i <= s.size(); i++) {
string temp = s.substr(curr, i - curr);
if (st.find(temp) != st.end()) {
path.push_back(temp);
backtracking(s, st, i, path);
path.pop_back();
}
}
}
vector<string> wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> st;
for (auto word : wordDict) st.insert(word);
vector<string> path;
backtracking(s, st, 0, path);
return res;
}
};