-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
44 lines (43 loc) · 1.16 KB
/
solution.h
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
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
set<string> S;
for (auto it : wordDict) {
S.insert(it);
}
vector<string> ans;
vector<string> now;
int n = s.size();
function<void(int)> dfs = [&](int idx) {
if (idx == n) {
string it = now[0];
for (int i = 1; i < now.size(); i++) {
it += " " + now[i];
}
ans.push_back(it);
return;
}
string tmp = "";
for (int i = idx; i < n; i++) {
tmp += s[i];
if (S.count(tmp)) {
now.push_back(tmp);
dfs(i + 1);
now.pop_back();
}
}
};
dfs(0);
return ans;
}
};