Valid Parentheses
class Solution {
public:
bool isValid(string s) {
unordered_map<char, char> map;
vector <char> stack;
map['('] = ')';
map['['] = ']';
map['{'] = '}';
for(auto i = s.begin(); i != s.end(); ++i){
if(map.find(*i) != map.end()){
stack.push_back(map[*i]);
}
else if (stack.empty() || stack.back() != (*i)){
return false;
}
else{
stack.pop_back();
}
}
return stack.empty();
}
};Last updated
Was this helpful?