Reverse Only Letters

Given a string `s`, reverse the string according to the following rules: - All the characters that are not English letters remain in the same position. - All the English letters (lowercase or uppercase) should be reversed. Return `s` after reversing it.

30:00

Reverse Only Letters
easy
Free written solution
Topics
Companies

Given a string s, reverse the string according to the following rules:

  • All the characters that are not English letters remain in the same position.
  • All the English letters (lowercase or uppercase) should be reversed.

Return s after reversing it.

Example 1:
Input: {"s":"ab-cd"}
Output: dc-ba
Constraints:
  • 1≤s.length≤1001 \leq s.\text{length} \leq 100

  • s consists of characters with ASCII values in the range [33, 122].

  • s does not contain '\"' or '\\'.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"ab-cd"}

4 of the 5 characters are letters. The other 1 are pinned (dashed) and never move; only letters trade places.

0

a
i

1

b

2

-
▴

3

c

4

d
j

i

0

j

4

letters settled

0/4

left pointer i
right pointer j
letters swapped now
letter in final place
non-letter, pinned (dashed)
Variables
VariableValue
i0
j4
chars[i]a
chars[j]d
swaps0
DepthFunction Call
Stack empty
0/5