Skip to content

Commit 6798e0e

Browse files
committed
word_break
1 parent 28c16f3 commit 6798e0e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
122122
#### [136. single number](https://github.com/hitzzc/go-leetcode/tree/master/single_number)
123123
#### [137. single number II](https://github.com/hitzzc/go-leetcode/tree/master/single_number_II)
124124
#### [138. copy_list_with_random_pointer](https://github.com/hitzzc/go-leetcode/tree/master/copy_list_with_random_pointer)
125+
#### [139. word break](https://github.com/hitzzc/go-leetcode/tree/master/word_break)
125126

126127

127128

word_break/word_break.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <string>
2+
#include <unordered_set>
3+
#include <vector>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
bool wordBreak(string s, unordered_set<string>& wordDict) {
9+
vector<bool> history(s.size(), false);
10+
for (int i = 0; i < s.size(); ++i){
11+
bool yes = false;
12+
for (int j = i; j >=0 && !yes; --j){
13+
if (wordDict.find(s.substr(j, i-j+1))!=wordDict.end() && (j == 0 || history[j-1]))
14+
yes = true;
15+
}
16+
history[i] = yes;
17+
}
18+
return history[s.size()-1];
19+
}
20+
};

0 commit comments

Comments
 (0)