diff --git a/Merge Strings Alternately - Leetcode 1768/Merge Strings Alternately - Leetcode 1768.py b/Merge Strings Alternately - Leetcode 1768/Merge Strings Alternately - Leetcode 1768.py index ffd2397..a79d5a5 100644 --- a/Merge Strings Alternately - Leetcode 1768/Merge Strings Alternately - Leetcode 1768.py +++ b/Merge Strings Alternately - Leetcode 1768/Merge Strings Alternately - Leetcode 1768.py @@ -1,28 +1,13 @@ class Solution: - def mergeAlternately(self, word1: str, word2: str) -> str: - A, B = len(word1), len(word2) - a, b = 0, 0 - s = [] - - word = 1 - while a < A and b < B: - if word == 1: - s.append(word1[a]) - a += 1 - word = 2 - else: - s.append(word2[b]) - b += 1 - word = 1 - - while a < A: - s.append(word1[a]) - a += 1 - - while b < B: - s.append(word2[b]) - b += 1 - - return ''.join(s) - # Time: O(A + B) - A is Length of word1, B is Length of word2 + def mergeAlternately(self, word1: str, word2: str) -> str: + l = 0 + res = "" + while l < len(word1) or l < len(word2): + if l < len(word1): + res += word1[l] + if l < len(word2): + res += word2[l] + l+=1 + return res + # Time: O(n) # Space: O(A + B) - A is Length of word1, B is Length of word2