Created
October 25, 2024 16:53
-
-
Save hongtaoh/791547ee832241a01c0bf92725f73c39 to your computer and use it in GitHub Desktop.
Solution to Leetcode 1768
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
""" | |
This is okay but unnecessarily complicated. | |
if a = [1, 2, 3], then a[4:] won't cause any errors. This will help me get a better solution. | |
""" | |
def mergeAlternately(self, word1: str, word2: str) -> str: | |
# len1 < len2 | |
len1, len2 = (len(word1), len(word2)) if len(word1) < len(word2) else (len(word2), len(word1)) | |
output = [] | |
for i in range(len1): | |
output.append(word1[i]) | |
output.append(word2[i]) | |
if len1 == len(word1): | |
output.append(word2[len1:]) | |
elif len1 != len2: | |
output.append(word1[len1:]) | |
return "".join(output) |
Author
hongtaoh
commented
Oct 25, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment