LeetCode 3612: Process String with Special Operations I – Java StringBuilder Solution Explained

Learn how to solve LeetCode 3612 using StringBuilder, string simulation, reverse operations, duplication logic, dry run examples, complexity analysis, and interview insights.

Krishna Shrivastava
3 views
LinkedInGithubX
0
0
LeetCode 3612: Process String with Special Operations I – Java StringBuilder Solution Explained
Listen to articleAudio version
Ad
Advertisement

Introduction

LeetCode 3612, Process String with Special Operations I, is an excellent string simulation problem that tests your ability to process characters sequentially while maintaining a dynamic result string.

The challenge introduces three special operations:

  1. * → Delete the last character
  2. # → Duplicate the current string
  3. % → Reverse the current string

Although the problem appears simple, it teaches an important interview concept:

Simulate operations exactly as described while efficiently modifying a string.

In this article, we'll break down the intuition, explore the optimal approach, perform a dry run, and analyze the time and space complexity.

Problem link :- Process String with Special Operations I

Problem Statement

You are given a string s containing:

  1. Lowercase English letters
  2. *
  3. #
  4. %

Process the string from left to right according to the following rules:

Lowercase Letter

Append it to the result.

a → result += 'a'


Asterisk (*)

Remove the last character if one exists.

abc*

becomes

ab


Hash (#)

Duplicate the current string.

abc#

becomes

abcabc


Percent (%)

Reverse the current string.

abc%

becomes

cba

Return the final string after processing all characters.


Example 1

Input

s = "a#b%*"

Step-by-Step Execution

CharacterOperationResult
aAppenda
#Duplicateaa
bAppendaab
%Reversebaa
*Remove lastba

Output

"ba"


Key Observation

The operations always modify the current result.

We need a data structure that supports:

Append

append()

Delete Last Character

deleteCharAt()

Reverse

reverse()

Duplicate

append(currentString)

A StringBuilder supports all of these efficiently.

This makes it the perfect choice for the problem.


Approach 1: StringBuilder Simulation

Intuition

Process each character one by one.

If it is a letter

Append it.

If it is *

Delete the last character.

If it is

Duplicate the entire current string.

If it is %

Reverse the current string.

Continue until all characters are processed.

Java Solution

class Solution {
public String processStr(String q) {
StringBuilder s = new StringBuilder();

for(int i =0;i < q.length();i++){

if(q.charAt(i) != '#'&&q.charAt(i) != '*'&& q.charAt(i) != '%'){

s.append(q.charAt(i));

}else if(q.charAt(i) == '#'&& s.length()>=1){

s.append(s);

}else if(q.charAt(i) == '*' && s.length()>=1){

s.deleteCharAt(s.length()-1);

}else{

s.reverse();

}

}

return s.toString();
}
}


Dry Run

Input

s = "abc#%"


Step 1

a

Result:

a


Step 2

b

Result:

ab


Step 3

c

Result:

abc


Step 4

#

Duplicate:

abcabc


Step 5

%

Reverse:

cbacba

Final Answer:

cbacba


Why StringBuilder Is Better Than String

Suppose we use:

result += ch;

Every append creates a brand-new string.

For large inputs this becomes inefficient.

StringBuilder modifies the same object in memory.

Benefits:

  1. Faster append
  2. Faster deletion
  3. Built-in reverse operation
  4. Lower memory overhead

This is why StringBuilder is the preferred interview solution.


Complexity Analysis

Let:

n = length of input string

Time Complexity

Append

O(1)

Delete Last

O(1)

Reverse

O(m)

where m is current result length.

Duplicate

O(m)

because the entire string is copied.

Overall:

O(n × m)

In this problem:

n ≤ 20

so this is perfectly acceptable.

Alternative Approach: Using a Stack

Another way is storing characters inside a stack.

Advantages:

  1. Easy handling of *
  2. Natural last-in-first-out behavior

Disadvantages:

  1. Reversing becomes expensive
  2. Duplicating requires rebuilding data

Therefore, StringBuilder remains the cleaner solution.

Interview Discussion

A common interview follow-up is:

Why not use String?

Because Strings are immutable.

Every operation like:

result += ch;

creates a new object.

StringBuilder avoids repeated object creation and is significantly more efficient.


Edge Cases

Case 1

s = "z*#"

Process:

z → "z"
* → ""
# → ""

Output:

""


Case 2

s = "%"

Output:

""

Reversing an empty string changes nothing.


Case 3

s = "*"

Output:

""

Nothing exists to delete.


Key Takeaways

  1. StringBuilder is ideal for string simulation problems.
  2. Process operations strictly from left to right.
  3. append(), deleteCharAt(), and reverse() make implementation simple.
  4. Always look for mutable string structures when frequent modifications are required.
  5. Simulation problems often test implementation accuracy more than algorithmic complexity.


Conclusion

LeetCode 3612: Process String with Special Operations I is a clean simulation problem that demonstrates how powerful StringBuilder can be for handling dynamic string transformations.

By processing each character sequentially and applying the required operation immediately, we can solve the problem efficiently with a straightforward and readable solution.

This problem is an excellent exercise for improving string manipulation skills and understanding when to choose mutable data structures over immutable strings.

Ai Assistant Kas