forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path139. Word Break.cpp
198 lines (138 loc) · 4.38 KB
/
139. Word Break.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
The same word in the dictionary may be reused multiple times in the segmentation.
You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
// Trie (TLE)
class Solution {
public:
struct TrieNode {
bool ew; // end of word
struct TrieNode* child[26];
};
TrieNode* createNode(){
TrieNode* newNode= new TrieNode;
newNode->ew=false;
for(int i=0;i<26;i++)
newNode->child[i]=NULL;
return newNode;
}
void insert(string word, TrieNode* root){
TrieNode* curr=root;
int wordLen=word.length();
for(int i=0;i<wordLen;i++){
int index=word[i]-'a';
if(curr->child[index]==NULL)
curr->child[index]=createNode();
curr=curr->child[index];
}
curr->ew=true;
}
bool search(string word, TrieNode* root){
TrieNode* curr=root;
int wordLen=word.length();
for(int i=0;i<wordLen;i++){
int index=word[i]-'a';
if(curr->child[index]==NULL) return false;
curr=curr->child[index];
}
return (curr!=NULL && curr->ew);
}
bool wordBreakUtil(string s, TrieNode* root){
int size=s.length();
if(size==0) return true;
for(int i=1;i<=size;i++){
if( search(s.substr(0, i), root) && wordBreakUtil(s.substr(i, size-i), root ))
return true;
}
return false;
}
bool wordBreak(string s, vector<string>& wordDict) {
int n=wordDict.size();
TrieNode* root= createNode();
for(int i=0;i<n;i++){
insert(wordDict[i], root);
}
return wordBreakUtil(s, root);
}
};
// Recursive (TLE)
class Solution {
public:
bool searchDict(string word, vector<string> &wordDict){
for(auto &x: wordDict)
if(word==x)
return true;
return false;
}
bool wordBreakUtil(string s, vector<string> &wordDict){
int size=s.length();
if(size==0) return true;
for(int i=1;i<=size;i++){
if(searchDict(s.substr(0, i), wordDict) && wordBreakUtil(s.substr(i, size-i), wordDict) )
return true;
}
return false;
}
bool wordBreak(string s, vector<string>& wordDict) {
return wordBreakUtil(s, wordDict);
}
};
// Recursive (memoized)
class Solution {
public:
unordered_map<string, bool> memo;
bool searchDict(string word, vector<string> &wordDict){
for(auto &x: wordDict)
if(word==x)
return true;
return false;
}
bool wordBreakUtil(string s, vector<string> &wordDict){
int size=s.length();
if(size==0) return true;
if(memo.find(s)!=memo.end()) return memo[s];
for(int i=1;i<=size;i++){
if(searchDict(s.substr(0, i), wordDict) && wordBreakUtil(s.substr(i, size-i), wordDict) )
return memo[s]=true;
}
return memo[s]=false;
}
bool wordBreak(string s, vector<string>& wordDict) {
return wordBreakUtil(s, wordDict);
}
};
// Dynamic Programming
// dp[i] denotes that the s.substr(0,i) is available in our dictionary
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
set<string>dictSet;
for(auto &x: wordDict)
dictSet.insert(x);
int n=s.length();
vector<bool> dp(n+1, false);
dp[0]=true;
for(int len=1;len<=n;len++){
for(int i=0;i<len;i++){
if(dp[i] && dictSet.find(s.substr(i, len-i))!=dictSet.end()){
dp[len]=true;
break;
}
}
}
return dp[n];
}
};