forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.h
More file actions
20 lines (19 loc) · 694 Bytes
/
Copy pathsolution.h
File metadata and controls
20 lines (19 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
using std::string;
#include <unordered_set>
using std::unordered_set;
#include <vector>
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
if (dict.find(s) != dict.end()) return true;
std::vector<string::const_iterator> cache{s.cbegin()};
for (auto subEnd = s.cbegin(); subEnd != s.cend(); ++subEnd)
for (auto subBeg : cache)
if (subBeg < subEnd && dict.find(string(subBeg, subEnd)) != dict.end()) {
if (dict.find(string(subEnd, s.cend())) != dict.end()) return true;
cache.push_back(subEnd); break;
}
return false;
}
};