HTMLify

LeetCode - Reverse Only Letters - Python
Views: 151 | Author: abh
class Solution:
    def reverseOnlyLetters(self, s: str) -> str:
        s = list(s)
        chars = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM"
        letters = []
        for c in s:
            if c in chars:
                letters.append(c)
        for i in range(len(s)):
            if s[i] in chars:
                s[i] = letters.pop()
        return "".join(s)

Comments