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:
result.'*' removes the last character from result, if it exists.'#' duplicates the current result and appends it to itself.'%' reverses the current result.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 <= 20s consists of only lowercase English letters and special characters *, #, and %.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.
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:
result.*, delete the last character in result (if it exists).#, copy result and append it to itself.%, reverse result.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).
Python
Java
C++
Go
TypeScript
| 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 |
Process String with Special Operations I || Leetcode Weekly Contest 458 • ExpertFunda • 639 views views
Watch 9 more video solutions →Practice Process String with Special Operations I with our built-in code editor and test cases.
Practice on FleetCode