
g0001_0100.s0020_valid_parentheses.Solution.cpp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Stack
// #Data_Structure_I_Day_9_Stack_Queue #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
// #2024_05_23_Time_0_ms_(100.00%)_Space_7.6_MB_(59.50%)
#include
#include
using namespace std;
class Solution {
public:
bool isValid(string s) {
stack stack;
for (char c : s) {
if (c == '(' || c == '[' || c == '{') {
stack.push(c);
} else if (c == ')' && !stack.empty() && stack.top() == '(') {
stack.pop();
} else if (c == '}' && !stack.empty() && stack.top() == '{') {
stack.pop();
} else if (c == ']' && !stack.empty() && stack.top() == '[') {
stack.pop();
} else {
return false;
}
}
return stack.empty();
}
};
© 2015 - 2025 Weber Informatics LLC | Privacy Policy