
Merge Strings Alternately (easy)
Problem Statement
Given two strings, word1 and word2, return the merged string after merging each character of both strings in alternate order, starting with the word1. If one string is longer than the other, append the remaining characters of the longer string to the end of the merged string.
Examples
-
Example 1:
- Input: word1:
"dog", word2:"cat" - Expected Output:
"dcoagt" - Justification: The characters from both strings are alternated, starting with the first character of the first string. Since both strings are of equal length, no extra characters remain at the end.
- Input: word1:
-
Example 2:
- Input: word1:
"hello", word2:"hi" - Expected Output:
"hheillo" - Justification: After alternating characters from both strings, the remaining characters from the longer string ("hello") are appended to the end.
- Input: word1:
-
Example 3:
- Input: word1:
"abc", word2:"xyz123" - Expected Output:
"axbycz123" - Justification: Characters are alternated, and since the second string is longer, the remaining characters ("123") are appended to the end.
- Input: word1:
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory