[LeetCode] Process String with Special Operations I

3612. Process String with Special Operations I

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
string processStr(string s) {
string res = "";
for(auto& ch : s) {
if(isalpha(ch)) res.push_back(ch);
else if(ch == '*') {
if(res.size()) res.pop_back();
} else if(ch == '#') res = res + res;
else if(ch == '%') {
reverse(begin(res), end(res));
}
}
return res;
}
};©leetcode

Author: Song Hayoung
Link: https://songhayoung.github.io/2025/07/14/PS/LeetCode/process-string-with-special-operations-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.