-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.wordbreak.js
More file actions
159 lines (141 loc) · 4.13 KB
/
string.wordbreak.js
File metadata and controls
159 lines (141 loc) · 4.13 KB
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
/*
* @title: String word break
* @description: check dictionary to see if string parts exist
* @author: Thorsten Kober
* @email: [email protected]
*/
/* Given an input string and a dictionary of words, find out if the input
* string can be segmented into a space-separated sequence of dictionary words. */
function dictionaryContains(str) {
const dictionary = ['mobile', 'samsung', 'sam', 'sung', 'man', 'mango', 'icecream', 'and', 'go', 'i', 'like', 'ice', 'cream'];
for (let i = 0; i < dictionary.length; i++) {
if (dictionary[i].indexOf(str) === 0 && str.length === dictionary[i].length) {
return true;
}
}
return false;
}
// recursion
function wordBreak(str) {
const size = str.length;
if (size === 0) return true;
// start at i equal to 1, stop at i equal to str.length
for (let i = 1; i <= size; i++) {
if (dictionaryContains(str.substr(0, i))
&& wordBreak(str.substr(i, size - i))) {
return true;
}
}
return false;
}
// tabulation
function wordBreak2(str) {
const result = [true];
for (let i = 1; i <= str.length; i++) {
result[i] = false;
}
for (let i = 1; i <= str.length; i++) {
for (let j = 0; j < i; j++) {
if (result[j] && dictionaryContains(str.substring(j, i))) {
result[i] = true;
break;
}
}
}
return result[str.length];
}
// tabulation
function wordBreak3(str) {
const size = str.length;
if (size === 0) return true;
const result = [];
for (let i = 0; i <= size; i++) {
result[i] = false;
}
// start at i equal to 1, stop at i equal to str.length
for (let i = 1; i <= size; i++) {
if (result[i] === false
&& dictionaryContains(str.substr(0, i))) {
result[i] = true;
}
if (result[i] === true) {
if (i === size) {
return true;
}
// start at j equal to i + 1, stop at j equal to j - i;
for (let j = i + 1; j <= size; j++) {
if (result[j] === false
&& dictionaryContains(str.substr(i, j - i))) {
result[j] = true;
}
if (result[j] === true) {
if (j === size) {
return true;
}
}
}
}
}
return false;
}
// npx jest algorithms/string/string.wordbreak.js
describe('wordbreak recursive', () => {
test('wordBreak(ilikesamsung)', () => {
expect(wordBreak('ilikesamsung')).toEqual(true);
});
test('wordBreak(iiiiiiii)', () => {
expect(wordBreak('iiiiiiii')).toEqual(true);
});
test('wordBreak()', () => {
expect(wordBreak('')).toEqual(true);
});
test('wordBreak(ilikelikeimangoiii)', () => {
expect(wordBreak('ilikelikeimangoiii')).toEqual(true);
});
test('wordBreak(samsungandmango)', () => {
expect(wordBreak('samsungandmango')).toEqual(true);
});
test('wordBreak(samsungandmangok)', () => {
expect(wordBreak('samsungandmangok')).toEqual(false);
});
});
describe('wordbreak tabulation', () => {
test('wordBreak2(ilikesamsung)', () => {
expect(wordBreak2('ilikesamsung')).toEqual(true);
});
test('wordBreak2(iiiiiiii)', () => {
expect(wordBreak2('iiiiiiii')).toEqual(true);
});
test('wordBreak2()', () => {
expect(wordBreak2('')).toEqual(true);
});
test('wordBreak2(ilikelikeimangoiii)', () => {
expect(wordBreak2('ilikelikeimangoiii')).toEqual(true);
});
test('wordBreak2(samsungandmango)', () => {
expect(wordBreak2('samsungandmango')).toEqual(true);
});
test('wordBreak2(samsungandmangok)', () => {
expect(wordBreak2('samsungandmangok')).toEqual(false);
});
});
describe('wordbreak tabulation', () => {
test('wordBreak3(ilikesamsung)', () => {
expect(wordBreak3('ilikesamsung')).toEqual(true);
});
test('wordBreak3(iiiiiiii)', () => {
expect(wordBreak3('iiiiiiii')).toEqual(true);
});
test('wordBreak3()', () => {
expect(wordBreak3('')).toEqual(true);
});
test('wordBreak3(ilikelikeimangoiii)', () => {
expect(wordBreak3('ilikelikeimangoiii')).toEqual(true);
});
test('wordBreak3(samsungandmango)', () => {
expect(wordBreak3('samsungandmango')).toEqual(true);
});
test('wordBreak3(samsungandmangok)', () => {
expect(wordBreak3('samsungandmangok')).toEqual(false);
});
});