Reverse Only Letters
Reverse only the letters in a string while every non-letter character stays exactly where it started - the same two-pointer swap, with a skip condition added on each side.
Problem Statement
Reverse only the letters in a string. Everything else (numbers, symbols, spaces) stays exactly where it is.
Brute Force
Pull out just the letters, reverse them, then place them back into the letter positions one by one. It works, but you're building extra lists to do it.
string reverseOnlyLetters(string s) {vector<char> letters;for (char c : s) {if (isalpha(c)) letters.push_back(c);}reverse(letters.begin(), letters.end());int j = 0;for (int i = 0; i < s.size(); i++) {if (isalpha(s[i])) {s[i] = letters[j++];}}return s;}
Time: O(n) Space: O(n) — the extra letters list is what we want to avoid.
Better Approach: Two Pointers
Same idea as reversing a plain string — one pointer at each end, moving inward. The only difference: before swapping, skip any character that isn't a letter.
string reverseOnlyLetters(string s) {int left = 0;int right = s.size() - 1;while (left < right) {if (!isalpha(s[left])) {left++;} else if (!isalpha(s[right])) {right--;} else {swap(s[left], s[right]);left++;right--;}}return s;}
Complexity
Every step moves a pointer forward — skip or swap — so we never revisit a character.
We still need to turn the string into a list of characters first, since strings can't be edited in place.
Forgetting to move the pointer in the skip step. If left or right doesn't
move past a non-letter, the loop never ends.
How to Explain It in an Interview
How to Explain it in an Interview
Only one side moves per step — check left first, then right. It still finishes correctly, just one step at a time.
The pointers just skip past everything until they cross, and nothing changes.