Reverse Letters Then Special Characters in a String - Solution & Explanation
Problem Statement
You are given a string s consisting of lowercase English letters and special characters.
Your task is to perform these in order:
- Reverse the lowercase letters and place them back into the positions originally occupied by letters.
- Reverse the special characters and place them back into the positions originally occupied by special characters.
Return the resulting string after performing the reversals.
Example 1:
Input: s = ")ebc#da@f("
Output: "(fad@cb#e)"
Explanation:
- The letters in the string are
['e', 'b', 'c', 'd', 'a', 'f']:- Reversing them gives
['f', 'a', 'd', 'c', 'b', 'e'] sbecomes")fad#cb@e("
- Reversing them gives
- ​​​​​​​The special characters in the string are
[')', '#', '@', '(']:- Reversing them gives
['(', '@', '#', ')'] sbecomes"(fad@cb#e)"
- Reversing them gives
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 <= 100sconsists only of lowercase English letters and the special characters in"!@#$%^&*()".
Approach Overview
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.
Solution
We first store the letters and special characters from string s into two separate lists a and b respectively. Then we traverse the string s. If the current position is a letter, we pop the last letter from list a and place it back at that position; otherwise, we pop the last special character from list b and place it back at that position.
After the traversal is complete, we obtain the result string.
The time complexity is O(n) and the space complexity is O(n), where n is the length of string s.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| 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 |
Video Solution
3823. Reverse Letters Then Special Characters in a String (Leetcode Easy) • Programming Live with Larry • 150 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Reverse Letters Then Special Characters in a String easy or hard?
Reverse Letters Then Special Characters in a String Python/Java solution
How to solve Reverse Letters Then Special Characters in a String in O(n)?
What is the best approach for Reverse Letters Then Special Characters in a String?
Is Reverse Letters Then Special Characters in a String asked at Google/Amazon/Meta?
What data structure is used in Reverse Letters Then Special Characters in a String?
What is the time complexity of Reverse Letters Then Special Characters in a String?
Ready to solve this problem?
Practice Reverse Letters Then Special Characters in a String with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor