Problem 830
Reverse Only Letters
Easy · 10 pts · String, Two Pointers
Problem
Reverse the order of the English letters in a string while every other character (digits, dashes and other symbols) stays exactly where it is. For example, ab-cd becomes dc-ba. Use two pointers from both ends: move each pointer past non-letters, swap the two letters they point at, and continue until the pointers meet.
Input
A single string S with no spaces.
Output
Print S with only its letters reversed.
Constraints
1 <= |S| <= 100000; S contains printable characters other than spaces
Example input
a-bC-dEf=ghIj
Example output
j-Ih-gfE=dCba
Explanation
In a-bC-dEf=ghIj, the letters in order are a b C d E f g h I j. Reversed they are j I h g f E d C b a, and putting them back into the letter positions gives j-Ih-gfE=dCba.