diff --git a/redandsilver/easy/ValidParentheses.cpp b/redandsilver/easy/ValidParentheses.cpp new file mode 100644 index 0000000..dba7a0d --- /dev/null +++ b/redandsilver/easy/ValidParentheses.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + bool isValid(string s) { + stack st; + for(char c : s){ + if(c=='(' || c=='{' || c=='['){ + st.push(c); + }else{ + if (st.empty() || + (c == ')' && st.top() != '(') || + (c == '}' && st.top() != '{') || + (c == ']' && st.top() != '[')) { + return false; + } + st.pop(); + } + } + return st.empty(); + } +}; \ No newline at end of file diff --git a/redandsilver/easy/ValidParentheses_2.cpp b/redandsilver/easy/ValidParentheses_2.cpp new file mode 100644 index 0000000..a761e39 --- /dev/null +++ b/redandsilver/easy/ValidParentheses_2.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + bool isValid(string s) { + unordered_map map; + map['('] =')'; + map['{'] ='}'; + map['['] =']'; + + stack st; + for(char c : s){ + if(c=='(' || c=='{' || c=='['){ + st.push(c); + }else{ + if(st.empty() || + map[st.top()] != c){ + return false; + } + st.pop(); + } + } + return st.empty(); + } +}; \ No newline at end of file