Reverse Only Letters - Problem

Imagine you're a proofreader working with a document where all the English letters need to be reversed while keeping all other characters (numbers, symbols, spaces) in their exact positions!

Given a string s, your task is to reverse only the English letters according to these rules:

  • All characters that are not English letters (a-z, A-Z) must remain in the same position
  • All English letters (both lowercase and uppercase) should be reversed among themselves

Goal: Return the modified string after reversing only the letters.

Example: "ab-cd" becomes "dc-ba" - notice how the dash stays in position 2, but the letters a,b,c,d become d,c,b,a.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "ab-cd"
โ€บ Output: "dc-ba"
๐Ÿ’ก Note: The letters are 'a', 'b', 'c', 'd'. When reversed, they become 'd', 'c', 'b', 'a'. The non-letter character '-' stays in position 2. So we get 'd' + 'c' + '-' + 'b' + 'a' = "dc-ba".
example_2.py โ€” Mixed Case with Numbers
$ Input: s = "a-bC-dEf-ghIj"
โ€บ Output: "j-Ih-gfE-dCba"
๐Ÿ’ก Note: Extract letters: a,b,C,d,E,f,g,h,I,j. Reversed: j,I,h,g,f,E,d,C,b,a. Place back while preserving positions of '-': "j-Ih-gfE-dCba".
example_3.py โ€” Edge Case - No Letters
$ Input: s = "Test1ng-Leet=code-Q!"
โ€บ Output: "Qedo1ct-eeLg=ntse-T!"
๐Ÿ’ก Note: Letters are: T,e,s,t,n,g,L,e,e,t,c,o,d,e,Q. Reversed: Q,e,d,o,c,t,e,e,L,g,n,t,s,e,T. Numbers and symbols stay in their positions.

Constraints

  • 1 โ‰ค s.length โ‰ค 100
  • s consists of printable ASCII characters
  • Letters include both uppercase and lowercase English letters (a-z, A-Z)

Visualization

Tap to expand
๐Ÿ›๏ธ Museum Gallery RearrangementOriginal Gallery LayoutLetterADisplay #1LetterBDisplay #2Symbol-Display #3LetterCDisplay #4LetterDDisplay #5Curators Working from Both EndsAB-CD๐Ÿ‘จโ€๐Ÿ’ผCurator L๐Ÿ‘ฉโ€๐Ÿ’ผCurator RSwap A โ†” DFinal Rearranged GalleryLetterDSwapped!LetterCSwapped!Symbol-UnchangedLetterBSwapped!LetterASwapped!๐ŸŽฏ Two Curator Strategyโœ… Only letter displays are rearrangedโœ… Symbol displays stay in placeโœ… Work from both ends toward middleโœ… Skip non-letter displaysโœ… Only swap when both find lettersResult: "DC-BA" โœจTime: O(n) | Space: O(n)
Understanding the Visualization
1
Survey the Gallery
Walk through and identify which display cases contain letters vs other artifacts
2
Two Curator Strategy
Position one curator at the entrance, another at the exit - they work toward the middle
3
Find and Swap
Each curator moves inward until finding a letter display, then they swap those artifacts
4
Continue Until Meeting
Repeat the process until both curators meet in the middle - all letters are now reversed!
Key Takeaway
๐ŸŽฏ Key Insight: The two-pointer approach elegantly handles this problem by treating letters and non-letters differently - we only swap letters while completely ignoring the positions of other characters. This results in optimal O(n) time complexity with a single pass through the string.
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
24.8K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen