Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
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 summary: 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 from result, if it exists. A '#' duplicates the current result and appends it to itself. A '%' reverses the current result. Return the final string result after processing all characters in s.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"a#b%*"
"z*#"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3612: Process String with Special Operations I
class Solution {
public String processStr(String s) {
StringBuilder result = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isLetter(c)) {
result.append(c);
} else if (c == '*') {
result.setLength(Math.max(0, result.length() - 1));
} else if (c == '#') {
result.append(result);
} else if (c == '%') {
result.reverse();
}
}
return result.toString();
}
}
// Accepted solution for LeetCode #3612: Process String with Special Operations I
func processStr(s string) string {
var result []rune
for _, c := range s {
if unicode.IsLetter(c) {
result = append(result, c)
} else if c == '*' {
if len(result) > 0 {
result = result[:len(result)-1]
}
} else if c == '#' {
result = append(result, result...)
} else if c == '%' {
slices.Reverse(result)
}
}
return string(result)
}
# Accepted solution for LeetCode #3612: Process String with Special Operations I
class Solution:
def processStr(self, s: str) -> str:
result = []
for c in s:
if c.isalpha():
result.append(c)
elif c == "*" and result:
result.pop()
elif c == "#":
result.extend(result)
elif c == "%":
result.reverse()
return "".join(result)
// Accepted solution for LeetCode #3612: Process String with Special Operations I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3612: Process String with Special Operations I
// class Solution {
// public String processStr(String s) {
// StringBuilder result = new StringBuilder();
// for (char c : s.toCharArray()) {
// if (Character.isLetter(c)) {
// result.append(c);
// } else if (c == '*') {
// result.setLength(Math.max(0, result.length() - 1));
// } else if (c == '#') {
// result.append(result);
// } else if (c == '%') {
// result.reverse();
// }
// }
// return result.toString();
// }
// }
// Accepted solution for LeetCode #3612: Process String with Special Operations I
function processStr(s: string): string {
const result: string[] = [];
for (const c of s) {
if (/[a-zA-Z]/.test(c)) {
result.push(c);
} else if (c === '*') {
if (result.length > 0) {
result.pop();
}
} else if (c === '#') {
result.push(...result);
} else if (c === '%') {
result.reverse();
}
}
return result.join('');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.