class Solution {
public:
bool isValid(string s) {
stack<char> sta;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(') {
sta.push(')');
} else if(s[i] == '[') {
sta.push(']');
} else if(s[i] == '{') {
sta.push('}');
} else if(!sta.empty() && s[i] == sta.top()){
sta.pop();
} else {
return false;
}
}
return sta.empty();
}
};
class Solution {
public:
stack<char> sta;
string removeDuplicates(string s) {
string str = "";
for(char ch : s) {
if (!str.empty() && ch == str.back()) {
str.pop_back();
} else {
str.push_back(ch);
}
}
return str;
}
};
class Solution {
public:
int evalRPN(vector<string>& tokens) {
int res = 0;
stack<int> st;
for (auto it : tokens) {
if (it == "+" || it == "-" || it == "*" || it == "/") {
int num2 = st.top();
st.pop();
int num1 = st.top();
st.pop();
if (it == "+") st.push(num1 + num2);
if (it == "-") st.push(num1 - num2);
if (it == "*") st.push(num1 * num2);
if (it == "/") st.push(num1 / num2);
} else {
st.push(stoi(it));
}
}
return st.top();
}
};