forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordBreak.cpp
62 lines (50 loc) · 1.4 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
// Source : https://oj.leetcode.com/problems/word-break/
// Author : Hao Chen
// Date : 2014-07-01
/**********************************************************************************
*
* Given a string s and a dictionary of words dict, determine if s can be segmented
* into a space-separated sequence of one or more dictionary words.
*
* For example, given
* s = "leetcode",
* dict = ["leet", "code"].
*
* Return true because "leetcode" can be segmented as "leet code".
*
*
**********************************************************************************/
#include <iostream>
#include <vector>
#include <set>
using namespace std;
bool wordBreak(string s, set<string> &dict) {
vector<bool> v(s.size(),false);
for(int i=0; i<s.size(); i++){
string w = s.substr(0,i+1);
v[i] = (dict.find(w)!=dict.end());
if (v[i]) continue;
for(int j=0; j<i; j++){
if (v[j]==true){
w = s.substr(j+1, i-j);
v[i] = (dict.find(w)!=dict.end());
if (v[i]) break;
}
}
}
return v.size() ? v[v.size()-1] : false;
}
int main()
{
string s;
set<string> dict;
s = "a";
dict.insert("a");
cout << wordBreak(s, dict) << endl;
dict.clear();
s = "dogs";
string d[]={"dog","s","gs"};
dict.insert(d, d+3);
cout << wordBreak(s, dict) << endl;
return 0;
}