-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergedStringCheck.js
107 lines (65 loc) · 2.64 KB
/
mergedStringCheck.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
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
/*
At a job interview, you are challenged to write an algorithm to check if a given string, s, can be formed from two other strings, part1 and part2.
The restriction is that the characters in part1 and part2 are in the same order as in s.
The interviewer gives you the following example and tells you to figure out the rest from the given test cases.
For example:
'codewars' is a merge from 'cdw' and 'oears':
s: c o d e w a r s = codewars
part1: c d w = cdw
part2: o e a r s = oears
https://www.codewars.com/kata/merged-string-checker/train/javascript
*/
function isMerge(str, part1, part2) {
var str = Array.from(str)
var part1 = Array.from(part1)
var part2 = Array.from(part2)
var countRight = 0
var countLeft = 0
var tryRight = function(str, part1, part2) {
if (str.length === 0 && str.length === part1.length && str.length === part2.length) {
return true
}
if (str.length !== (part1.length + part2.length)) {
return false
}
var newStr = str.slice(1, str.length)
var newPart1 = part1.slice(1, part1.length)
var newPart2 = part2.slice(1, part2.length)
if (str[0] === part1[0] && str !== "") {
return (tryRight(newStr, newPart1, part2) || tryLeft(newStr, newPart1, part2))
}
if (str[0] === part2[0] && str !== "") {
return (tryLeft(newStr, part1, newPart2) || tryRight(newStr, part1, newPart2))
}
return false
}
var tryLeft = function(str, part1, part2) {
if (str.length === 0 && str.length === part1.length && str.length === part2.length) {
return true
}
if (str.length !== part1.length + part2.length) {
return false
}
var newStr = str.slice(1, str.length)
var newPart1 = part1.slice(1, part1.length)
var newPart2 = part2.slice(1, part2.length)
if (str[0] === part2[0] && str !== "") {
return (tryLeft(newStr, part1, newPart2) || tryRight(newStr, part1, newPart2))
}
if (str[0] === part1[0] && str !== "") {
return (tryRight(newStr, newPart1, part2) || tryLeft(newStr, newPart1, part2))
}
return false
}
var check = function(str, part1, part2) {
if (tryRight(str, part1, part2) || tryLeft(str, part1, part2)) {
return true
}
return false
}
var result = check(str, part1, part2)
//console.log(result);
return result
}
//console.log(isMerge("Bananas from Bahamas", "Bahas", "Bananas from am"));
console.log(isMerge("codewars", "code", "wasr"));