-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeoverlappingstrings.js
More file actions
42 lines (31 loc) · 945 Bytes
/
Copy pathMergeoverlappingstrings.js
File metadata and controls
42 lines (31 loc) · 945 Bytes
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
// This kata requires you to write a function which merges two strings together. It does so by merging the end of the first string with the start of the second string together when they are an exact match.
// "abcde" + "cdefgh" => "abcdefgh"
// "abaab" + "aabab" => "abaabab"
// "abc" + "def" => "abcdef"
// "abc" + "abc" => "abc"
function mergeStrings(first, second){
let count = 0;
for (let i = 0; i < first.length; i++) {
if (first[i] === second[count]) {
count++;
} else {
count = 0;
if (first[i] === second[count])
count++;
}
}
return first + second.slice(count);
}
//different approach
function mergeStrings(first, second){
let res = '';
if( first === second) {
return first;
}
for(let i = 1; i < second.length; i++) {
if(first.endsWith(second.slice(0, i))) {
res = second.slice(i);
}
}
return !res ? first + second : first + res;
}