-
Notifications
You must be signed in to change notification settings - Fork 985
/
Copy pathwordBreakAllMethods.cpp
212 lines (203 loc) · 5.1 KB
/
wordBreakAllMethods.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
node *children[26];
bool isEOW;
} node;
node *createNode()
{
node *newNode = (node *)malloc(sizeof(node));
for (int i = 0; i < 26; i++)
newNode->children[i] = NULL;
newNode->isEOW = false;
return newNode;
}
void insertNode(node *root, string key)
{
node *cur = root;
for (int i = 0; i < key.length(); i++)
{
int index = key[i] - 'a';
if (!cur->children[index])
cur->children[index] = createNode();
cur = cur->children[index];
}
cur->isEOW = true;
}
bool searchKey(node *root, string key)
{
node *cur = root;
for (int i = 0; i < key.length(); i++)
{
int index = key[i] - 'a';
if (!cur->children[index])
return false;
cur = cur->children[index];
}
return cur && cur->isEOW;
}
bool dictionaryContains(vector<string> dictionary, string str)
{
for (int i = 0; i < dictionary.size(); i++)
{
if (dictionary[i].compare(str) == 0)
return true;
}
return false;
}
bool wordBreakRecursive(vector<string> dictionary, string str)
{
int size = str.size();
if (size == 0)
return true;
for (int i = 1; i <= size; i++)
{
if (dictionaryContains(dictionary, str.substr(0, i)) && wordBreakRecursive(dictionary, str.substr(i, size - i)))
return true;
}
return false;
}
bool wordBreakDp4(vector<string> dictionary, string str)
{
int size = str.size();
vector<int> dp(size + 1, 0);
dp[0] = 1;
for (int i = 0; i < size; i++)
{
if (dp[i])
{
for (auto w : dictionary)
{
if (str.substr(i, w.size()) == w)
dp[i + w.size()] = 1;
}
}
}
return dp[size];
}
bool wordBreakDp2(vector<string> dictionary, string str)
{
int size = str.size();
vector<bool> dp(size + 1, 0);
vector<int> matchedIndex;
matchedIndex.push_back(-1);
for (int i = 0; i < size; i++)
{
int f = 0;
int msize = matchedIndex.size();
for (int j = msize - 1; j >= 0; j--)
{
string sub = str.substr(matchedIndex[j] + 1, i - matchedIndex[j]);
if (dictionaryContains(dictionary, sub))
{
f = 1;
break;
}
}
if (f)
{
dp[i] = 1;
matchedIndex.push_back(i);
}
}
return dp[size - 1];
}
bool wordBreakDp3(node *root, string str)
{
int size = str.size();
vector<bool> dp(size + 1, 0);
vector<int> matchedIndex;
matchedIndex.push_back(-1);
for (int i = 0; i < size; i++)
{
int f = 0;
int msize = matchedIndex.size();
for (int j = msize - 1; j >= 0; j--)
{
string sub = str.substr(matchedIndex[j] + 1, i - matchedIndex[j]);
if (searchKey(root, sub))
{
f = 1;
break;
}
}
if (f)
{
dp[i] = 1;
matchedIndex.push_back(i);
}
}
return dp[size - 1];
}
bool wordBreakDp(vector<string> dictionary, string str)
{
int size = str.size();
int j;
if (size == 0)
return true;
vector<int> dp(size + 1, 0);
for (int i = 1; i <= size; i++)
{
if (!dp[i] && dictionaryContains(dictionary, str.substr(0, i)))
dp[i] = 1;
if (dp[i])
{
if (i == size)
return true;
for (j = i + 1; j <= size; j++)
{
if (!dp[j] && dictionaryContains(dictionary, str.substr(i, j - i)))
dp[j] = 1;
}
if (dp[i] && size == j)
return true;
}
}
return false;
}
int main()
{
string str;
cout << "Enter string" << endl;
cin >> str;
vector<string> dictionary = {"mobile", "samsung", "sam", "sung", "man", "mango", "icecream", "and", "go", "i", "like", "ice", "cream"};
//1
if (wordBreakRecursive(dictionary, str))
cout << "true" << endl;
else
cout << "false" << endl;
//2
if (wordBreakDp(dictionary, str))
cout << "true" << endl;
else
cout << "false" << endl;
//3
if (wordBreakDp2(dictionary, str))
cout << "true" << endl;
else
cout << "false" << endl;
//3
node *root = createNode();
for (int i = 0; i < dictionary.size(); i++)
insertNode(root, dictionary[i]);
if (wordBreakDp3(root, str))
cout << "true" << endl;
else
cout << "false" << endl;
//4
if (wordBreakDp4(dictionary, str))
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
/*
- wordBreak: https://www.geeksforgeeks.org/word-break-problem-dp-32/
- methods
- recursive: Tc:(2^s \*n ), Sc:(n^2): recursive calls. s=length of string,n=length of dictionary
- Dp: Tc:O(s^2 \* n), Sc: o(s)
- Dp2: Tc:O(s\*n) Sc:O(s)
- Dp3: similar to Dp2, for checking whether the string present in dictionary use trie.
- Dp4: Tc: O(s\*n\*maxWordLengthOfDict)
*/