#917

Reverse Only Letters

pupil · 300 · lc easy +20 · verified · 68.2% accepted · 2,440 likes · top 75%

Description

Given a string s, return a new string where all English letters (a–z, A–Z) are reversed relative to their original positions, while every non-letter character remains exactly where it was.

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!"

Code

1
2
3