-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsolution.js
50 lines (42 loc) · 1.09 KB
/
solution.js
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
var prefs, roots, suffs;
var init = function(data) {
var d = data.split("|");
prefs = d[0].split(",");
roots = d[1].split(",");
suffs = d[2].split(",");
}
var test = function(word) {
var result = false;
if (roots.indexOf(word) >= 0)
{
return true;
}
roots.forEach(function(x) {
if (word.indexOf(x) >= 0) {
var parts = word.split(x, 3);
if (isContains(parts[0], prefs) && (isContains(parts[1], suffs)||test(parts[1]))) {
result = true;
}
}
});
return result;
}
//check that 'word' contains parts only from 'dict'
function isContains(word, dict) {
if (word.length == 0) {
return true;
}
var old = word.length;
var result = false;
dict.forEach(function(x) {
var c = word.replace(x, "");
if (c.length < old) {
if (isContains(c,dict)) {
result = true;
}
}
});
return result;
}
module.exports.init = init;
module.exports.test = test;