/
word break problem.cpp
181 lines (141 loc) Β· 3.38 KB
/
word break problem.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
/*
//////////////////////////////////////////////////////
//Question/Info
Word Break
Medium Accuracy: 50.24% Submissions: 13735 Points: 4
Given a string A and a dictionary of n words B, find out if A can be segmented into a space-separated sequence of dictionary words.
Example 1:
Input:
n = 12
B = { "i", "like", "sam", "sung", "samsung", "mobile",
"ice","cream", "icecream", "man", "go", "mango" }
A = "ilike"
Output: 1
Explanation:The string can be segmented as "i like".
Γ’β¬βΉExample 2:
Input:
n = 12
B = { "i", "like", "sam", "sung", "samsung", "mobile",
"ice","cream", "icecream", "man", "go", "mango" }
A = "ilikesamsung"
Output: 1
Explanation: The string can be segmented as
"i like samsung" or "i like sam sung".
Your Task:
Complete wordBreak() function which takes a string and list of strings as a parameter and returns 1 if it is possible to break words, else return 0. You don't need to read any input or print any output, it is done by driver code.
Expected time complexity: O(s2)
Γ’β¬βΉExpected auxiliary space: O(s) , where s = length of string A
Constraints:
1 <= N <= 12
1 <= s <=1000 , where s = length of string A
The length of each word is less than 15.
Company Tags
Amazon Google Hike IBM MAQ Software Microsoft Walmart Zoho
author: srj_v
//////////////////////////////////////////////////////
*/
#include <bits/stdc++.h>
using namespace std;
#define _IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long double ld;
typedef long long int lli;
#pragma GCC optimize("Ofast")
void c_p_c()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////////////////////
int32_t main() {
///////////
// c_p_c();
///////////
_IOS
//////////
// code
/*
int t ; cin >> t; while(t--){}
*/
//User function template for C++
// A : given string to search
// B : vector of available strings
int wordBreak(string A, vector<string> &B) {
//code here
if (B.size() == 0 or A.length() == 0) return false;
vector<bool> dp(A.size() + 1, false);
dp[0] = true;
for (int i = 1; i <= A.size(); i++)
{
for (int j = i - 1; j >= 0; j--)
{
if (dp[j]) // NOTE HERE DP[J]
{
string word = A.substr(j, i - j);
auto it = find(B.begin(), B.end(), word);
if (it != B.end())
{
dp[i] = true; // NOTE HERE DP[I]
break; //next i
}
}
}
}
return dp[A.size()];
// ~ O(N^2 * S) complexity...
// its similar to .....
// leetcode
// -
// leetcode
// -
// --
// leetcode
// -
// --
// ---
// leetcode
// -
// --
// ---
// ---- !!! leet
// leetcode
// -
// --
// ---
// ----
// -----
// leetcode
// -
// --
// ---
// ----
// -----
// ------
// leetcode
// -
// --
// ---
// ----
// -----
// ------
// -------
// leetcode
// -
// --
// ---
// ---- !!! code
// All the lines below won't run b/c of break;
// Index 4 is a valid start index, and index 4 - index 7 make up the word `code`
// We can break once we've found any connecting word because
// all we're trying to do is find SOME valid way to connect to s[i].
// Once we've found SOME word that does that, there's no need to continue checking.
// -----
// ------
// -------
// --------
}
// cerr << "time: " << clock() << " ms" << '\n';
return 0;
}
//////////////////////////////////////////////////////