-
Notifications
You must be signed in to change notification settings - Fork 0
/
OverlapString.js
31 lines (27 loc) · 874 Bytes
/
OverlapString.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
// Given two strings, find the overlap of the end of the first
// string, and the beginning of the second string, if any. If
// there is no overlap, return an empty string
//
// Example:
// firstString = 'I like t'
// secondSTring = 'e turtles'
// return 'e t'
//findOverlap('I like t', 'e turtles')
function findOverlap(firstString, secondString) {
var firstStringArr = firstString.split('');
var overlapString = '';
var index = -1;
var previousIndex = -1;
firstStringArr.forEach(e => {
index = secondString.indexOf(e);
if (index == 0) {
overlapString = e;
previousIndex = 0;
}
if (previousIndex > -1 && previousIndex + 1 == index) {
overlapString = `${overlapString}${e}`;
previousIndex = index;
}
});
return overlapString;
}