Leetcode 1532 Solution

This article provides solution to leetcode question 1532 (reformat-the-string)

https://leetcode.com/problems/reformat-the-string

Solution

class Solution:
    def reformat(self, s: str) -> str:
        s1 = "".join([ch for ch in s if '0' <= ch <= '9'])
        s2 = "".join([ch for ch in s if 'a' <= ch <= 'z'])

        def join_str(s1, s2):
            s = ""
            for ch1, ch2 in zip(s1, s2):
                s += ch1 + ch2
            return s

        if len(s1) == len(s2):
            return join_str(s1, s2)
        elif len(s1) == len(s2) + 1:
            return join_str(s1[:-1], s2) + s1[-1]
        elif len(s1) + 1 == len(s2):
            return join_str(s2[:-1], s1) + s2[-1]
        else:
            return ""