-
Notifications
You must be signed in to change notification settings - Fork 0
/
139-WordBreak.cpp
49 lines (43 loc) · 1.19 KB
/
139-WordBreak.cpp
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
49
/*=============================================================================
# FileName: 139-WordBreak.cpp
# Desc:
# Author: qsword
# Email: huangjian1993@gmail.com
# HomePage:
# Created: 2015-05-10 08:43:09
# Version: 0.0.1
# LastChange: 2015-05-10 08:53:18
# History:
# 0.0.1 | qsword | init
=============================================================================*/
#include <stdio.h>
#include <unordered_set>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
//15ms
bool wordBreak(string s, unordered_set<string>& wordDict) {
int len = s.length();
if (len < 0) {
return true;
}
vector<bool> dp(len + 1, false);
dp[0] = true;
for (int i = 1; i <= len; i ++) {
for (int j = 0; j < i; j ++) {
if (dp[j] && wordDict.find(s.substr(j, i - j)) != wordDict.end()) {
dp[i] = true;
break;
}
}
}
return dp[len];
}
};
int main() {
unordered_set<string> dict;
Solution solution;
printf("%d\n", solution.wordBreak("a", dict));
}