-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombinetwostrings.js
55 lines (48 loc) · 1.68 KB
/
combinetwostrings.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
"use strict";
/*We are given 3 strings: str1, str2, and str3. Str3 is said to be a shuffle of str1 and str2 if it can be formed by interleaving the characters of str1 and str2 in a way that maintains the left to right ordering of the characters from each string. For example, given str1=”abc” and str2=”def”, str3=”dabecf” is a valid shuffle since it preserves the character ordering of the two strings. So, given these 3 strings write a function that detects whether str3 is a valid shuffle of str1 and str2. - See more at: http://www.ardendertat.com/2012/01/09/programming-interview-questions/#sthash.oVkqXXAW.dpuf*/
function verifyCombinationString(str1, str2, str3) {
if(!str3 || !str2 || !str1) {
return false;
}
var arrStr1 = str1.split(""),
arrStr2 = str2.split(""),
arrStr3 = str3.split(""),
str1Len = arrStr1.length,
str2Len = arrStr2.length,
str3Len = arrStr3.length;
if(str3Len !== (str1Len + str2Len)) {
return false;
}
for(var index = 0, src1Index = 0, src2Index = 0; index < str3Len; ) {
var c = arrStr3[index],
nextC;
if(src1Index < str1Len) {
if(src2Index < str2Len) {
nextC = arrStr3[index + 1];
if((c == arrStr1[src1Index] && nextC == arrStr2[src2Index]) || (c == arrStr2[src2Index] && nextC == arrStr1[src1Index])) {
index += 2;
src1Index++;
src2Index++;
} else {
return false;
}
} else if(c == arrStr1[src1Index]) {
index++;
src1Index++;
} else {
return false;
}
} else if(src2Index < str2Len) {
if(c == arrStr2[src2Index]) {
index++;
src2Index++;
} else {
return false;
}
} else {
return false;
}
}
return true;
}
module.exports = verifyCombinationString;