Reverse Only Letters
Question
Given a string
S
, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions.
Example 1:
Input: "ab-cd" Output: "dc-ba"
Example 2:
Input: "a-bC-dEf-ghIj" Output: "j-Ih-gfE-dCba"
Example 3:
Input: "Test1ng-Leet=code-Q!" Output: "Qedo1ct-eeLg=ntse-T!"
Note:
- S.length <= 100
- 33 <= S[i].ASCIIcode <= 122
- S doesn't contain
\
or"
Approach 1: Stack of Letters
Intuition and Algorithm
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.
1 | class Solution(object): |
Complexity Analysis
- Time Complexity:O(N), where N is the
length of
S
. - Space Complexity: O(N).
- Runtime: 16 ms, faster than 92.70% of Python online submissions for Reverse Only Letters.
- Memory Usage: 11.8 MB, less than 51.06% of Python online submissions for Reverse Only Letters.