Skip to content
Decoder

Reverse Only Letters

Easy

Problem

Given a string s, reverse only the English letters (a–z and A–Z) and return the result. Every character that is not a letter stays at the same index.

Example 1

Input
s = "ab-cd"
Output
"dc-ba"

Example 2

Input
s = "a-bC-dEf-ghIj"
Output
"j-Ih-gfE-dCba"

Example 3

Input
s = "Test1ng-Leet=code-Q!"
Output
"Qedo1ct-eeLg=ntse-T!"

Constraints

  • 1 <= s.length <= 100
  • s consists of ASCII characters with codes from 33 to 122.
  • s contains no double quotes or backslashes.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.