Watch 5 video solutions for Reverse Letters Then Special Characters in a String, a easy level problem involving Two Pointers, String, Simulation. This walkthrough by Programming Live with Larry has 127 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s consisting of lowercase English letters and special characters.
Your task is to perform these in order:
Return the resulting string after performing the reversals.
Example 1:
Input: s = ")ebc#da@f("
Output: "(fad@cb#e)"
Explanation:
['e', 'b', 'c', 'd', 'a', 'f']:
['f', 'a', 'd', 'c', 'b', 'e']s becomes ")fad#cb@e("[')', '#', '@', '(']:
['(', '@', '#', ')']s becomes "(fad@cb#e)"Example 2:
Input: s = "z"
Output: "z"
Explanation:
The string contains only one letter, and reversing it does not change the string. There are no special characters.
Example 3:
Input: s = "!@#$%^&*()"
Output: ")(*&^%$#@!"
Explanation:
The string contains no letters. The string contains all special characters, so reversing the special characters reverses the whole string.
Constraints:
1 <= s.length <= 100s consists only of lowercase English letters and the special characters in "!@#$%^&*()".Problem Overview: Given a string containing letters and special characters, reorder it so the letters appear in reverse order and the special characters also appear in reverse order, while each group keeps its own positions relative to the other group. The task is mostly about scanning the string carefully and swapping characters that belong to the same category.
Approach 1: Simulation with Extra Storage (O(n) time, O(n) space)
The straightforward approach separates the problem into two collections. First iterate through the string and push all letters into one list and all special characters into another. Reverse both lists. Then iterate through the original string again and rebuild the result: whenever the current position contains a letter, take the next element from the reversed letter list; otherwise take from the reversed special-character list. This works because each character category is processed independently while the string traversal keeps their placement pattern intact.
Approach 2: Two Pointers Simulation (O(n) time, O(1) space)
A more space‑efficient solution uses two pointers. First collect indices of letters and reverse them in-place by moving pointers from both ends of the letter index list and swapping characters in the original string. Repeat the same logic for special characters. Each swap operation exchanges characters that belong to the same category, ensuring letters only swap with letters and special characters only swap with special characters. Because each character is visited a constant number of times, the algorithm runs in linear time.
This pattern is common in two pointers problems where you selectively swap elements that satisfy a condition. It also fits the idea of simulation: you mimic the exact transformation step by step rather than deriving a complex formula. The core operations are simple string traversal, classification using character checks, and controlled swapping.
Both implementations rely heavily on basic string manipulation. The key insight is to treat letters and special characters as two independent sequences embedded inside the same string. Once you isolate those sequences logically, reversing them becomes a standard operation.
Recommended for interviews: The two‑pointer simulation approach is typically expected. It achieves O(n) time with O(1) extra space and demonstrates that you can manipulate strings efficiently in-place. Showing the extra‑array simulation first can help explain the idea, but the in‑place two‑pointer version signals stronger problem‑solving ability.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simulation with Extra Arrays | O(n) | O(n) | Simplest implementation; useful for explaining the logic before optimizing |
| Two Pointers In-Place Simulation | O(n) | O(1) | Best general solution when memory usage should stay constant |