forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordBreak.cpp
67 lines (55 loc) · 1.71 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
63
64
65
66
67
// 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) {
//using an array to mark subarray from 0 to i can be broken or not
vector<bool> v(s.size(),false);
for(int i=0; i<s.size(); i++){
//check the substring from 0 to i is int dict or not
string w = s.substr(0,i+1);
v[i] = (dict.find(w)!=dict.end());
//if it is, then use greedy algorithm
if (v[i]) continue;
//if it is not, then break it to check
for(int j=0; j<i; j++){
//if the substring from 0 to j can be borken, then check the substring from j to i
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;
}