July 24, 2024

Viiisit [LeetCode] - 1768. Merge Strings Alternately

#php#python#javascript#ruby

keep learning, keep coding!

Problem - Merge Strings Alternately

You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.

Return the merged string.

翻譯蒟蒻

word1word2 字串的字母以交錯順序合併,從 word1 開始。如果其中一個字串比另一個字串長,則將多餘的字母直接附加到合併後的字串末尾。



Solution - JavaScript

Solution 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var mergeAlternately = function(word1, word2) {
let result = "";
let i = 0;
let j = 0;
while (i < word1.length && j < word2.length) {
result += word1[i] + word2[j];
i++;
j++;
}
while (i < word1.length) {
result += word1[i];
i++;
}
while (j < word2.length) {
result += word2[j];
j++;
}
return result;
};

Solution 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var mergeAlternately = function(word1, word2) {
let result = "";
if (word1.length > word2.length) {
for (let i = 0; i < word2.length; i++) {
result += word1[i] + word2[i];
}
result += word1.slice(word2.length);
} else {
for (let i = 0; i < word1.length; i++) {
result += word1[i] + word2[i];
}
result += word2.slice(word1.length);
}
return result;
};

Solution - Ruby

Solution 1:

1
2
3
4
5
6
7
8
9
10
11
def merge_alternately(word1, word2)
i, j = 0, 0
result = ""
while i < word1.length || j < word2.length
result += word1[i] if i < word1.length
result += word2[j] if j < word2.length
i += 1
j += 1
end
result
end

Solution 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# @param {String} word1
# @param {String} word2
# @return {String}
def merge_alternately(word1, word2)
result = ""
if word1.length > word2.length
for i in 0..word2.length-1
result += word1[i] + word2[i]
end
result += word1[word2.length..-1]
else
for i in 0..word1.length-1
result += word1[i] + word2[i]
end
result += word2[word1.length..-1]
end
return result
end

Solution - PHP

Solution 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function mergeAlternately($word1, $word2) {
$i = 0;
$j = 0;
$result = '';
while ($i < strlen($word1) && $j < strlen($word2)) {
$result .= $word1[$i];
$result .= $word2[$j];
$i++;
$j++;
}
while ($i < strlen($word1)) {
$result .= $word1[$i];
$i++;
}
while ($j < strlen($word2)) {
$result .= $word2[$j];
$j++;
}
return $result;
}

Solution 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function mergeAlternately($word1, $word2) {
$result = "";
if (strlen($word1) > strlen($word2)) {
for ($i = 0; $i < strlen($word2); $i++) {
$result .= $word1[$i] . $word2[$i];
}
$result .= substr($word1, $i);
} else {
for ($i = 0; $i < strlen($word1); $i++) {
$result .= $word1[$i] . $word2[$i];
}
$result .= substr($word2, $i);
}
return $result;
}

Solution - Python3

Solution 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
def mergeAlternately(self, word1: str, word2: str) -> str:
result = ""
i = 0
j = 0
while i < len(word1) and j < len(word2):
result += word1[i] + word2[j]
i += 1
j += 1
if i < len(word1):
result += word1[i:]
if j < len(word2):
result += word2[j:]
return result

Solution 2:

1
2
3
4
5
6
7
8
9
10
11
def mergeAlternately(self, word1: str, word2: str) -> str:
result = ""
if len(word1) > len(word2):
for i in range(len(word2)):
result += word1[i] + word2[i]
result += word1[len(word2):]
else:
for i in range(len(word1)):
result += word1[i] + word2[i]
result += word2[len(word1):]
return result

LeetCode 傳送門 - Merge Strings Alternately