-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordBreak.txt
28 lines (24 loc) · 958 Bytes
/
wordBreak.txt
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
139. Word Break
Given a string s and a dictionary of strings wordDict,
return true if s can be segmented into a space-separated sequence of one or more dictionary words.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
class Solution {
public:
bool solve(int start, string s, unordered_set<string>& dict, vector<int> &dp) {
int length = s.length();
if(start == length) return true;
if(dp[start] != -1) return dp[start];
for(int i = start ; i < length ; i++){
if(dict.count(s.substr(start,i - start + 1)) && solve(i+1 ,s ,dict, dp)){
dp[start] = 1;
return true;
}
}
return dp[start] = false;
}
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> dict(wordDict.begin(),wordDict.end());
vector<int> dp(s.size(),-1);
return solve(0,s,dict,dp);
}
};