Skip to content

Commit 81c9122

Browse files
committed
✅ [1768]
1 parent c15a547 commit 81c9122

File tree

2 files changed

+67
-28
lines changed

2 files changed

+67
-28
lines changed

1768/my_solution.js

+40-15
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,52 @@
44
* @return {string}
55
*/
66
var mergeAlternately = function (word1, word2) {
7-
let str = "";
7+
let str = "", processWord = [], appendWord = []
8+
89
word1 = word1.split(""), word2 = word2.split("");
9-
while (word1.length > 0 || word2.length > 0) {
10-
let curr1 = "", curr2 = "";
11-
console.log(word1)
12-
console.log(word2)
13-
if (word1.length > 0) {
14-
curr1 = word1.shift();
15-
}
16-
17-
if (word2.length > 0) {
18-
curr2 = word2.shift();
19-
}
20-
21-
str += curr1 + curr2;
10+
// use the short one to process
11+
// if (word1.length <= word2.length) {
12+
processWord = word1;
13+
appendWord = word2;
14+
// } else {
15+
// processWord = word2;
16+
// appendWord = word1;
17+
// }
18+
19+
for (let i = 0; i < word1.length; i++) {
20+
console.log("word1")
21+
console.log(word1[i])
22+
str += word1[i];
23+
if (word2.length > 0) str += word2.shift();
2224
}
2325

26+
console.log("str")
27+
console.log(str)
28+
29+
str += word2.join("");
30+
31+
// while (word1.length > 0 || word2.length > 0) {
32+
// let curr1 = "", curr2 = "";
33+
// console.log(word1)
34+
// console.log(word2)
35+
// if (word1.length > 0) {
36+
// curr1 = word1.shift();
37+
// }
38+
//
39+
// if (word2.length > 0) {
40+
// curr2 = word2.shift();
41+
// }
42+
//
43+
// str += curr1 + curr2;
44+
// }
45+
2446
return str;
2547
};
48+
2649
let x =
27-
mergeAlternately("abc", "pqr")
50+
// mergeAlternately("abc", "pqr")
51+
mergeAlternately("ab", "pqrs")
52+
// mergeAlternately("abcd", "pq")
2853

2954
console.log("Res")
3055
console.log(x)

1768/solution.js

+27-13
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,35 @@
55
*/
66
var mergeAlternately = function (word1, word2) {
77
let str = "";
8-
word1 = word1.split(""), word2 = word2.split("");
9-
while (word1.length > 0 || word2.length > 0) {
10-
let curr1 = "", curr2 = "";
11-
console.log(word1)
12-
console.log(word2)
13-
if (word1.length > 0) {
14-
curr1 = word1.shift();
15-
}
16-
17-
if (word2.length > 0) {
18-
curr2 = word2.shift();
19-
}
208

21-
str += curr1 + curr2;
9+
word1 = word1.split(""), word2 = word2.split("");
10+
for (let i = 0; i < word1.length; i++) {
11+
str += word1[i];
12+
if (word2.length > 0) str += word2.shift();
2213
}
2314

15+
str += word2.join("");
16+
2417
return str;
2518
};
19+
20+
// var mergeAlternately = function (word1, word2) {
21+
// let str = "";
22+
// word1 = word1.split(""), word2 = word2.split("");
23+
// while (word1.length > 0 || word2.length > 0) {
24+
// let curr1 = "", curr2 = "";
25+
// console.log(word1)
26+
// console.log(word2)
27+
// if (word1.length > 0) {
28+
// curr1 = word1.shift();
29+
// }
30+
//
31+
// if (word2.length > 0) {
32+
// curr2 = word2.shift();
33+
// }
34+
//
35+
// str += curr1 + curr2;
36+
// }
37+
//
38+
// return str;
39+
// };

0 commit comments

Comments
 (0)