Skip to content

Instantly share code, notes, and snippets.

@Robogeek95
Created October 14, 2020 20:49
Show Gist options
  • Save Robogeek95/6798a1c3475abb125424418725fc17fd to your computer and use it in GitHub Desktop.
Save Robogeek95/6798a1c3475abb125424418725fc17fd to your computer and use it in GitHub Desktop.
20. Valid Parentheses
class Solution {
public:
bool isValid(string s) {
stack<char> parentheses;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') parentheses.push(s[i]);
else {
if (parentheses.empty()) return false;
if (s[i] == ')' && parentheses.top() != '(') return false;
if (s[i] == ']' && parentheses.top() != '[') return false;
if (s[i] == '}' && parentheses.top() != '{') return false;
parentheses.pop();
}
}
return parentheses.empty();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment