Process String with Special Operations I
Problem Statement
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 fromresult, if it exists (backspace). - A
#duplicates the currentresultand appends it to itself. - A
%reverses the currentresult.
Return the final string result after processing all characters in s.
Example 1:
- Input:
s = "a#b%*" - Output:
"ba" - Explanation:
'a'-> Append:"a"'#'-> Duplicate:"aa"'b'-> Append:"aab"'%'-> Reverse:"baa"'*'-> Remove last:"ba"
Example 2:
- Input:
s = "z*#" - Output:
"" - Explanation:
'z'-> Append:"z"'*'-> Remove last:""'#'-> Duplicate:""
Approach: Simulation
Since the constraints on the length of the string are extremely small (), we can solve this by directly simulating the operations using a dynamic array, list, or string builder depending on the language.
- Maintain an empty
resultstructure. - Iterate through each character of the input string
s. - Based on the character, perform the respective operation:
- Lowercase letter (
a-z): Add to the end of theresult. - Asterisk (
*): Pop the last element (make sure to check if theresultis not empty first). - Hash (
#): Concatenate the currentresultto itself. - Percent (
%): Reverse the characters in theresult.
- Lowercase letter (
- Once the loop finishes, return the final
resultcasted back to a string.
Using a list or character array is typically more efficient for these operations than manipulating immutable strings directly, especially for the reverse and duplicate steps.
Complexity Analysis
- Time Complexity:
In the absolute worst-case scenario, the string size can double at each step (e.g., if the input is mostly
#characters). For an input length , the maximum length of the output string and the maximum number of operations required to reverse or duplicate the string would be proportional to . - Space Complexity:
The space required to store the intermediate and final
resultstring can grow exponentially, up to characters.
Solutions
C++
#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
string processStr(string s) {
string ans = "";
for (char c : s) {
if (c >= 'a' && c <= 'z') {
ans.push_back(c);
} else if (c == '*') {
if (!ans.empty()) {
ans.pop_back();
}
} else if (c == '#') {
ans += ans;
} else if (c == '%') {
reverse(ans.begin(), ans.end());
}
}
return ans;
}
};
Java
class Solution {
public String processStr(String s) {
StringBuilder ans = new StringBuilder();
for (char c : s.toCharArray()) {
if (c >= 'a' && c <= 'z') {
ans.append(c);
} else if (c == '*') {
if (ans.length() > 0) {
ans.deleteCharAt(ans.length() - 1);
}
} else if (c == '#') {
ans.append(ans);
} else if (c == '%') {
ans.reverse();
}
}
return ans.toString();
}
}
Python
class Solution:
def processStr(self, s: str) -> str:
ans = []
for c in s:
if 'a' <= c <= 'z':
ans.append(c)
elif c == '*':
if ans:
ans.pop()
elif c == '#':
ans.extend(ans)
elif c == '%':
ans.reverse()
return "".join(ans)
JavaScript
/**
* @param {string} s
* @return {string}
*/
var processStr = function(s) {
let ans = [];
for (let c of s) {
if (c >= 'a' && c <= 'z') {
ans.push(c);
} else if (c === '*') {
if (ans.length > 0) {
ans.pop();
}
} else if (c === '#') {
ans = ans.concat(ans);
} else if (c === '%') {
ans.reverse();
}
}
return ans.join("");
};
Telemetry Integration
Completed working through this block? Sync progress to workspace.