Leetcode 917 Reverse Only Letters

Given a string s, reverse the string according to the following rules:

  • All the characters that are not English letters remain in the same position.
  • All the English letters (lowercase or uppercase) should be reversed. Return s after reversing it.
Input: s = "ab-cd"
Output: "dc-ba"
Input: s = "a-bC-dEf-ghIj"
Output: "j-Ih-gfE-dCba"
  • Collect the letters of S separately into a stack, so that popping the stack reverses the letters. (Alternatively, we could have collected the letters into an array and reversed the array.)

  • Then, when writing the characters of S, any time we need a letter, we use the one we have prepared instead.

class Solution(object):
    def reverseOnlyLetters(self, s: str) -> str:
        letters = [c for c in s if c.isalpha()]
        ans = []
        for c in s:
            if c.isalpha():
                ans.append(letters.pop())
            else:
                ans.append(c)
        return "".join(ans)