Skip to content

Reverse Only Letters

EasyTwo Pointers
Decode it first. Clues, constraints, pattern and plan before any code.

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.

s = "ab-cd"                  →  "dc-ba"
s = "a-bC-dEf-ghIj"          →  "j-Ih-gfE-dCba"
s = "Test1ng-Leet=code-Q!"   →  "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.