File tree 1 file changed +52
-0
lines changed
1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < algorithm>
2
+ #include < cmath>
3
+ #include < iostream>
4
+ #include < limits>
5
+ #include < map>
6
+ #include < numeric>
7
+ #include < queue>
8
+ #include < set>
9
+ #include < stack>
10
+ #include < string>
11
+ #include < unordered_map>
12
+ #include < unordered_set>
13
+ #include < vector>
14
+
15
+ using namespace std ;
16
+
17
+ class Solution {
18
+ public:
19
+ bool wordBreak (string s, vector<string>& wordDict) {
20
+ move (wordDict.begin (), wordDict.end (), inserter (word_set, word_set.begin ()));
21
+ return backtrack (s, 0 );
22
+ }
23
+
24
+ private:
25
+ bool backtrack (const string& s, int begin) {
26
+ if (begin >= (int )s.length ()) {
27
+ return true ;
28
+ }
29
+
30
+ if (memorization.count (begin)) {
31
+ return false ;
32
+ }
33
+
34
+ for (int i = begin; i < (int )s.length (); ++i) {
35
+ string substr = s.substr (begin, i - begin + 1 );
36
+ if (word_set.count (substr) && backtrack (s, i + 1 )) {
37
+ return true ;
38
+ }
39
+ }
40
+
41
+ memorization.insert (begin);
42
+ return false ;
43
+ }
44
+
45
+ private:
46
+ unordered_set<string> word_set;
47
+ unordered_set<int > memorization;
48
+ };
49
+
50
+ int main () {
51
+ return 0 ;
52
+ }
You can’t perform that action at this time.
0 commit comments