2025-08-15 13:35:39 +0000 UTC

Sorting the Sentence

Code

class Solution:
    def sortSentence(self, s: str) -> str:
        res = [None] * 9
        cur = []
        for char in s:
            if char.isalpha():
                cur.append(char)
            elif char.isdigit():
                res[int(char) - 1] = "".join(cur)
                cur.clear()
        return " ".join(word for word in res if word is not None)