Reverse the letters, leave the rest
Reported in interviews at (where)
- Spire Technologies
Reverse the order of the letters in s, the characters a to z and A to Z, while every other character (digits, punctuation, spaces) stays exactly where it is.
Return the result.
- Input
s = "Ab,c$de"- Output
"ed,c$bA"
The letters A, b, c, d, e come back as e, d, c, b, A in the same five places; "," and "$" stay put.
- Input
s = "7-up!"- Output
"7-pu!"
Only u and p swap.
Limits
1 <= s.length <= 100000s holds printable ASCII characters
Hints
Hint 1
Move two pointers inwards. Each one steps over anything that is not a letter; when both stand on letters, swap them.
Where this was reported
- Spire Technologies, SDE interview, 2024 (GeeksforGeeks)
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.