Skip to main content

Reverse Letters Then Special Characters in a String - Solution & Explanation

Practice this problem

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']
    • s becomes ")fad#cb@e("
  • ​​​​​​​The special characters in the string are [')', '#', '@', '(']:
    • Reversing them gives ['(', '@', '#', ')']
    • 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 <= 100
  • s consists 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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simulation with Extra ArraysO(n)O(n)Simplest implementation; useful for explaining the logic before optimizing
Two Pointers In-Place SimulationO(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?
This problem is typically classified as Easy. The main challenge is careful string traversal and correctly identifying which characters belong to the letter group versus the special-character group while reversing each group.
Reverse Letters Then Special Characters in a String Python/Java solution
In Python or Java, convert the string to a mutable character array, identify characters that belong to the same category, and swap them using two pointers moving toward the center. The same logic works in C++, Go, and TypeScript with identical O(n) time complexity.
How to solve Reverse Letters Then Special Characters in a String in O(n)?
Traverse the string and identify positions of letters and special characters. Use two pointers for each category, swapping characters from the left and right ends of that category's index list. Each swap reverses the order within the group, producing the final string in linear time.
What is the best approach for Reverse Letters Then Special Characters in a String?
The best approach uses a two-pointer simulation that reverses characters belonging to the same category. You identify indices of letters and special characters and swap them from both ends so each group reverses independently. This method runs in O(n) time and uses O(1) extra space.
Is Reverse Letters Then Special Characters in a String asked at Google/Amazon/Meta?
String manipulation and two-pointer simulation questions appear frequently in interviews at companies like Amazon, Google, and Meta. Variations that reverse characters under specific constraints are common because they test careful iteration, condition checks, and in-place updates.
What data structure is used in Reverse Letters Then Special Characters in a String?
The solution mainly relies on basic string or character array manipulation. Some implementations temporarily store indices or characters in arrays or stacks, but the optimal version only uses two pointers and performs swaps directly in the string.
What is the time complexity of Reverse Letters Then Special Characters in a String?
The optimal solution runs in O(n) time because each character in the string is visited a constant number of times during classification and swapping. The in-place two-pointer approach also uses O(1) extra space, while simpler simulation versions may require O(n) auxiliary storage.

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 FleetCode