Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit e5299b4

Browse files
committed
Merge pull request #28 from YoussefKababe/master
Fix and optimize code
2 parents 1267b63 + ca53dfc commit e5299b4

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed
+10-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
var combineTwoStrings = function (str1, str2, str3) {
22
var pos1 = 0,
33
pos2 = 0;
4-
4+
55
if (str1 + str2 === str3) { return true; }
6-
6+
77
for (var i = 0; i < str3.length; i++) {
8-
// If both the next characters match, we need to look ahead to the next
9-
// value and decide which path to take
10-
if (str3[i] === str1[pos1] && str3[i] === str2[pos2]) {
11-
if (str3[i + 1] === str1[pos1 + 1]) {
12-
pos1 += 1;
13-
} else {
14-
pos2 += 1;
15-
}
16-
} else if (str3[i] === str1[pos1]) {
17-
pos1 += 1;
18-
} else if (str3[i] === str2[pos2]) {
19-
pos2 += 1;
20-
} else {
8+
if (str3[i] === str1[pos1]) {
9+
pos1++;
10+
}
11+
else if (str3[i] === str2[pos2]) {
12+
pos2++;
13+
}
14+
else if (str3.length - i - 1 < str1.length - pos1 + str2.length - pos2) {
2115
return false;
22-
}
16+
}
2317
}
24-
2518
return true;
2619
};

0 commit comments

Comments
 (0)