Leetcode 1880 Solution

This article provides solution to leetcode question 1880 (largest-merge-of-two-strings)

https://leetcode.com/problems/largest-merge-of-two-strings

Solution

class Solution:
    def largestMerge(self, word1: str, word2: str) -> str:
        i = 0
        j = 0

        ans = ""
        while i < len(word1) or j < len(word2):
            if i == len(word1):
                ans += word2[j:]
                break
            elif j == len(word2):
                ans += word1[i:]
                break
            elif word1[i:] >= word2[j:]:
                ans += word1[i]
                i += 1
            elif word1[i:] < word2[j:]:
                ans += word2[j]
                j += 1

        return ans