You are given a string s consisting of lowercase English letters and the special characters: '*', '#', and '%'.
You are also given an integer k.
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 kth character of the final string result. If k is out of the bounds of result, return '.'.
Example 1:
Input: s = "a#b%*", k = 1
Output: "a"
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" |
The final result is "ba". The character at index k = 1 is 'a'.
Example 2:
Input: s = "cd%#*#", k = 3
Output: "d"
Explanation:
i |
s[i] |
Operation | Current result |
|---|---|---|---|
| 0 | 'c' |
Append 'c' |
"c" |
| 1 | 'd' |
Append 'd' |
"cd" |
| 2 | '%' |
Reverse result |
"dc" |
| 3 | '#' |
Duplicate result |
"dcdc" |
| 4 | '*' |
Remove the last character | "dcd" |
| 5 | '#' |
Duplicate result |
"dcddcd" |
The final result is "dcddcd". The character at index k = 3 is 'd'.
Example 3:
Input: s = "z*#", k = 0
Output: "."
Explanation:
i |
s[i] |
Operation | Current result |
|---|---|---|---|
| 0 | 'z' |
Append 'z' |
"z" |
| 1 | '*' |
Remove the last character | "" |
| 2 | '#' |
Duplicate the string | "" |
The final result is "". Since index k = 0 is out of bounds, the output is '.'.
Constraints:
1 <= s.length <= 105s consists of only lowercase English letters and special characters '*', '#', and '%'.0 <= k <= 1015result after processing s will not exceed 1015.Problem Overview: You receive a string and a set of special operations that modify the current sequence while scanning it. Some operations append characters, others remove or transform previously processed parts. The goal is to simulate these rules and return the final string after all operations are applied.
Approach 1: Direct Simulation with Mutable String (O(n^2) time, O(n) space)
The most straightforward way is to simulate every operation exactly as described. Iterate through the input string and maintain a mutable result string. When a special symbol appears, apply its effect immediately: append characters, remove the last character, or rebuild the string depending on the rule. This approach is simple but inefficient because operations like reversing or repeated concatenation can repeatedly copy the string, leading to O(n^2) time in the worst case. It works for small inputs but will likely time out for large constraints.
Approach 2: Stack / Deque Based Simulation (O(n) time, O(n) space)
A more efficient method treats the result as a dynamic structure such as a stack or deque. Iterate through the characters and push normal characters into the structure. When an operation appears, update the structure instead of rebuilding the entire string. For example, a delete-style operation simply performs a stack pop, while insertions become push operations. If the operation changes ordering (like reversing behavior), track the direction using a flag and push to the front or back accordingly. Each character is processed at most once, which keeps the complexity at O(n). This pattern is a classic combination of string processing and simulation.
Approach 3: Lazy Operation Tracking (O(n) time, O(n) space)
When operations repeatedly affect the entire string (such as toggling direction or applying batch transformations), rebuilding after every step is expensive. Instead, record the operation state lazily. Maintain flags or counters representing the current transformation state, and only apply them when inserting or extracting characters. A deque works well here because you can append on both ends while respecting the current direction. This reduces repeated work and keeps each step constant time. The approach often appears in advanced stack and simulation problems where operations interact with previously processed characters.
Recommended for interviews: Interviewers expect the linear-time simulation using a stack or deque. The brute-force approach demonstrates you understand the rules of the operations, but the optimized simulation shows you can control time complexity and avoid repeated string rebuilding. Mention that each character is processed once and that all operations translate to constant-time stack or deque updates.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct String Simulation | O(n^2) | O(n) | Good for understanding the rules or very small inputs |
| Stack / Deque Simulation | O(n) | O(n) | General case where operations modify recent characters |
| Lazy Operation Tracking | O(n) | O(n) | Best when operations frequently affect string direction or global state |
Leetcode 3614 | Process String with Special Operations II Detailed Explanation | Dry Run • Samrat Bhardwaj • 799 views views
Watch 6 more video solutions →Practice Process String with Special Operations II with our built-in code editor and test cases.
Practice on FleetCode