Process String with Special Operations I - Solution & Explanation
Problem Statement
You are given a string s consisting of lowercase English letters and the special characters: *, #, and %.
Build a new string result by processing s according to the following rules from left to right:
- If the letter is a lowercase English letter append it to
result. - A
'*'removes the last character fromresult, if it exists. - A
'#'duplicates the currentresultand appends it to itself. - A
'%'reverses the currentresult.
Return the final string result after processing all characters in s.
Example 1:
Input: s = "a#b%*"
Output: "ba"
Explanation:
i |
s[i] |
Operation | Current result |
|---|---|---|---|
| 0 | 'a' |
Append 'a' |
"a" |
| 1 | '#' |
Duplicate result |
"aa" |
| 2 | 'b' |
Append 'b' |
"aab" |
| 3 | '%' |
Reverse result |
"baa" |
| 4 | '*' |
Remove the last character | "ba" |
Thus, the final result is "ba".
Example 2:
Input: s = "z*#"
Output: ""
Explanation:
i |
s[i] |
Operation | Current result |
|---|---|---|---|
| 0 | 'z' |
Append 'z' |
"z" |
| 1 | '*' |
Remove the last character | "" |
| 2 | '#' |
Duplicate the string | "" |
Thus, the final result is "".
Constraints:
1 <= s.length <= 20sconsists of only lowercase English letters and special characters*,#, and%.
Approach Overview
Problem Overview: You receive a string that contains normal characters mixed with special operation characters. Each operation modifies the current result string (for example deleting, reversing, or altering previously processed characters). The task is to process the string from left to right and return the final string after applying every operation.
Approach 1: Naive String Simulation (O(n²) time, O(n) space)
The most direct approach is to simulate the process using a mutable string and apply operations immediately as they appear. Iterate through the input and append regular characters to the result string. When a special operation appears, modify the string accordingly (for example removing the last character or reversing the entire string). Operations like full reversal or repeated string concatenation can cost O(n) each, which pushes the worst‑case complexity to O(n²). This approach is simple and mirrors the problem statement, but it becomes slow when the string grows large.
Approach 2: Optimized Simulation with Stack / String Builder (O(n) time, O(n) space)
A more efficient strategy uses a dynamic container such as a stack, list, or StringBuilder. Iterate through the input exactly once. Regular characters are appended to the container. When an operation appears, apply the corresponding modification directly to the container (for example pop for deletion operations or toggling a flag for operations like reversal). By avoiding repeated full-string rebuilds, every character is processed at most once, giving O(n) time complexity and O(n) auxiliary space.
Many implementations also use a boolean flag to represent logical reversal instead of physically reversing the string each time. Characters are appended to either the front or back depending on the flag, typically using a deque. This technique keeps each operation constant time while preserving the correct order of characters.
This problem is primarily a simulation exercise combined with careful string manipulation from the string category. The key idea is to represent the evolving result efficiently and apply operations without repeatedly rebuilding the entire string.
Recommended for interviews: The optimized simulation using a stack, deque, or string builder. Interviewers expect you to process the string in one pass and avoid expensive operations like repeated reversal or concatenation. Showing the naive simulation demonstrates understanding, but the O(n) implementation proves you can reason about performance and data structure choices.
Solution
We can directly simulate the operations described in the problem. We use a list result to store the current result string. For each character in the input string s, we perform the corresponding operation based on the character type:
- If the character is a lowercase English letter, add it to
result. - If the character is
*, delete the last character inresult(if it exists). - If the character is
#, copyresultand append it to itself. - If the character is
%, reverseresult.
Finally, we convert result to a string and return it.
The time complexity is O(2^n), where n is the length of string s. In the worst case, the # operation may cause the length of result to double each time, resulting in exponential time complexity. Ignoring the space consumption of the answer, the space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Naive String Simulation | O(n²) | O(n) | Simple implementation when constraints are small or when first reasoning about the problem |
| Stack / String Builder Simulation | O(n) | O(n) | Best general solution; processes the string in one pass without rebuilding intermediate results |
| Deque with Reverse Flag | O(n) | O(n) | Useful when operations frequently reverse the string; avoids expensive full reversals |
Video Solution
Process String with Special Operations I | Simple Simulation | Leetcode 3612 | codestorywithMIK • codestorywithMIK • 2,993 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Process String with Special Operations I easy or hard?
Process String with Special Operations I Python/Java solution
How to solve Process String with Special Operations I in O(n)?
What is the best approach for Process String with Special Operations I?
Is Process String with Special Operations I asked at Google/Amazon/Meta?
What data structure is used in Process String with Special Operations I?
What is the time complexity of Process String with Special Operations I?
Ready to solve this problem?
Practice Process String with Special Operations I with our built-in code editor and test cases.
Practice on FleetCode