-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord_Break.cpp
More file actions
29 lines (29 loc) · 900 Bytes
/
Word_Break.cpp
File metadata and controls
29 lines (29 loc) · 900 Bytes
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
class Solution {
public:
bool recursive(string s, unordered_set<string>& ust){
int size = s.size();
if(size==0)
return true;
bool dp[size+1];
memset(dp,0,sizeof(dp));
for(int i=1; i<=size; i++){
if(!dp[i] && (ust.find(s.substr(0,i))!=ust.end()))
dp[i] = true;
if(dp[i]){
if(i==size)
return true;
for(int j=i+1; j<=size; j++){
if(!dp[j] && (ust.find(s.substr(i,j-i))!=ust.end()))
dp[j] = true;
if(j==size && dp[j])
return true;
}
}
}
return false;
}
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> ust(wordDict.begin(), wordDict.end());
return recursive(s,ust);
}
};