-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path139.cpp
More file actions
48 lines (42 loc) · 972 Bytes
/
Copy path139.cpp
File metadata and controls
48 lines (42 loc) · 972 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <unordered_map>
#include "common.h"
using namespace std;
class Solution {
public:
unordered_map<string, bool> memo;
bool wordBreak(string s, vector<string>& wordDict) {
unordered_map<char, vector<string>> candidate;
for (auto& s : wordDict) {
candidate[s[0]].push_back(s);
}
return helper(candidate, s);
}
bool helper(unordered_map<char, vector<string>>& candidate, string s) {
if (s.length() == 0) {
return true;
}
if (memo.count(s)) {
return memo[s];
}
int rst = false;
const vector<string>& check = candidate[s.at(0)];
for (auto& word : check) {
string sub_str = s.substr(0, word.size());
if (sub_str != word) {
continue;
}
if (helper(candidate, s.substr(word.size()))) {
rst = true;
break;
}
}
memo[s] = rst;
return rst;
}
};
int main() {
Solution s;
vector<string> strs;
s.wordBreak("", strs);
return 0;
}