Skip to main content

Process String with Special Operations II - Video Solutions

HardStringSimulation

Process String with Special Operations II | Simplified | Dry Runs | Leetcode 3614 | codestorywithMIK

codestorywithMIK
48:1315,751 views
10 video solutions available

Process String with Special Operations II - Video Solution

Watch 10 video solutions for Process String with Special Operations II, a hard level problem involving String, Simulation. This walkthrough by codestorywithMIK has 15,751 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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:

  • If the letter is a lowercase English letter append it to result.
  • A '*' removes the last character from result, if it exists.
  • A '#' duplicates the current result and appends it to itself.
  • A '%' 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 <= 105
  • s consists of only lowercase English letters and special characters '*', '#', and '%'.
  • 0 <= k <= 1015
  • The length of result after processing s will not exceed 1015.
Read full problem with examples

Approach Overview

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.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct String SimulationO(n^2)O(n)Good for understanding the rules or very small inputs
Stack / Deque SimulationO(n)O(n)General case where operations modify recent characters
Lazy Operation TrackingO(n)O(n)Best when operations frequently affect string direction or global state