Untitled
user_9124840
plain_text
a year ago
641 B
8
Indexable
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for (char ch : s) {
if (ch == '(' || ch == '{' || ch == '[') {
st.push(ch);
} else {
if (st.empty())
return false;
if (ch == ')' && st.top() != '(')
return false;
if (ch == '}' && st.top() != '{')
return false;
if (ch == ']' && st.top() != '[')
return false;
st.pop();
}
}
return st.empty();
}
};Editor is loading...
Leave a Comment