/**
* Problem: LeetCode 3612 - Process String with Special Operations I
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public String processStr(String s) {
StringBuilder result = new StringBuilder();
for (char ch : s.toCharArray()) {
if (ch >= 'a' && ch <= 'z') {
result.append(ch);
}
else if (ch == '*') {
if (result.length() > 0) {
result.deleteCharAt(result.length() - 1);
}
}
else if (ch == '#') {
result.append(result.toString());
}
else if (ch == '%') {
result.reverse();
}
}
return result.toString();
}
}
"""
Problem: LeetCode 3612 - Process String with Special Operations I
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
class Solution:
def processStr(self, s: str) -> str:
result = []
for ch in s:
if "a" <= ch <= "z":
result.append(ch)
elif ch == "*":
if result:
result.pop()
elif ch == "#":
result += result
elif ch == "%":
result.reverse()
return "".join(result)
/**
* Problem: LeetCode 3612 - Process String with Special Operations I
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 3612 - Process String with Special Operations I
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
string processStr(string s) {
string result = "";
for (char ch : s) {
if (ch >= 'a' && ch <= 'z') {
result.push_back(ch);
} else if (ch == '*') {
if (!result.empty()) {
result.pop_back();
}
} else if (ch == '#') {
result += result;
} else if (ch == '%') {
reverse(result.begin(), result.end());
}
}
return result;
}
};
/**
* Problem: LeetCode 3612 - Process String with Special Operations I
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var processStr = function(s) {
let result = [];
for (let i = 0; i < s.length; i++) {
const ch = s[i];
if (ch >= 'a' && ch <= 'z') {
result.push(ch);
} else if (ch === '*') {
if (result.length > 0) {
result.pop();
}
} else if (ch === '#') {
result = result.concat(result);
} else if (ch === '%') {
result.reverse();
}
}
return result.join('');
};