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.
Example 1:
Input: word1 = "abc ", word2 = "pqr " Output: "apbqcr " Explanation: The merged string will be merged as so: word1: a b c word2: p q r merged: a p b q c r
Example 2:
Input: word1 = "ab ", word2 = "pqrs " Output: "apbqrs " Explanation: Notice that as word2 is longer, "rs " is appended to the end. word1: a b word2: p q r s merged: a p b q r s
Example 3:
Input: word1 = "abcd ", word2 = "pq " Output: "apbqcd " Explanation: Notice that as word1 is longer, "cd " is appended to the end. word1: a b c d word2: p q merged: a p b q c d
Constraints:
1 <= word1.length, word2.length <= 100
word1
and word2
consist of lowercase English letters.program main
implicit none
character(len=100) :: word1, word2
character(len=200) :: merged
! Example 1
word1 = 'abc '
word2 = 'pqr '
call merge_strings(word1, word2, merged)
print *, trim(merged)
! Example 2
word1 = 'ab '
word2 = 'pqrs '
call merge_strings(word1, word2, merged)
print *, trim(merged)
! Example 3
word1 = 'abcd '
word2 = 'pq '
call merge_strings(word1, word2, merged)
print *, trim(merged)
contains
subroutine merge_strings(word1, word2, merged)
implicit none
character(len=*), intent(in) :: word1, word2
character(len=*), intent(out) :: merged
integer :: i, j, k
merged = ''
i = 1
j = 1
do while (i <= len(word1) .and. j <= len(word2))
merged = merged // word1(i:i) // word2(j:j)
i = i + 1
j = j + 1
end do
if (i <= len(word1)) then
merged = merged // word1(i:)
else if (j <= len(word2)) then
merged = merged // word2(j:)
end if
end subroutine merge_strings
end program main
program AlternatingMerge
implicit none
character(len=100) :: word1, word2, merged
integer :: i, j, k
! Read input
read(*,*) word1
read(*,*) word2
! Initialize variables
i = 1
j = 1
k = 1
! Merge the strings
do while (i <= len(word1) .and. j <= len(word2))
merged(k:k) = word1(i:i)
merged(k+1:k+1) = word2(j:j)
i = i + 1
j = j + 1
k = k + 2
end do
! Add remaining letters of the longer string
if (len(word1) > len(word2)) then
merged(k:k+len(word1)-len(word2)-1) = word1(i:len(word1))
else if (len(word2) > len(word1)) then
merged(k:k+len(word2)-len(word1)-1) = word2(j:len(word2))
end if
! Print output
print *, merged
end program AlternatingMerge
At line 8 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7f8717432960 in ??? #1 0x7f87174334d9 in ??? #2 0x7f871768717b in ??? #3 0x7f8717680684 in ??? #4 0x7f87176812aa in ??? #5 0x55af4577420c in MAIN__ #6 0x55af45774393 in main
def mergeAlternately(word1: str, word2: str) -> str:
result = []
i, j = 0, 0
while i < len(word1) and j < len(word2):
result.append(word1[i])
result.append(word2[j])
i += 1
j += 1
result += word1[i:]
result += word2[j:]
return "".join(result)
The algorithm initializes the result as an empty string (or an empty list in Python) and two pointers i
and j
to track the index of the current element in word1
and word2
, respectively. Then, it iterates through the words, adding one character from each of the input words alternatively to the result. The loop stops when one of the words run out of characters.
After the loop, the remaining characters in the input strings (if any) are appended to the result. In Python, the result is converted back to a string using the join()
method.
std::string mergeAlternately(std::string word1, std::string word2) {
std::string result = "";
int i = 0, j = 0;
while (i < word1.length() && j < word2.length()) {
result += word1[i++];
result += word2[j++];
}
while (i < word1.length()) result += word1[i++];
while (j < word2.length()) result += word2[j++];
return result;
}
The algorithm initializes the result as an empty string (or an empty list in Python) and two pointers i
and j
to track the index of the current element in word1
and word2
, respectively. Then, it iterates through the words, adding one character from each of the input words alternatively to the result. The loop stops when one of the words run out of characters.
After the loop, the remaining characters in the input strings (if any) are appended to the result. In Python, the result is converted back to a string using the join()
method.