diff --git a/problems/valid-parentheses.cpp b/problems/valid-parentheses.cpp new file mode 100644 index 0000000..3b8e767 --- /dev/null +++ b/problems/valid-parentheses.cpp @@ -0,0 +1,32 @@ +#include +#include + +class Solution { +public: + bool isValid(const std::string& s) + { + std::vector st; + + for (auto c : s) { + switch (c) { + case '(': + st.push_back(')'); + break; + case '{': + st.push_back('}'); + break; + case '[': + st.push_back(']'); + break; + default: + if (st.empty() || st.back() != c) { + return false; + } + st.pop_back(); + break; + } + } + + return st.empty(); + } +};