diff --git a/Intermediate.txt b/Intermediate.txt index f0f1651..52d436f 100644 --- a/Intermediate.txt +++ b/Intermediate.txt @@ -1768,3 +1768,21 @@ public class MergeSort { } } + +37. Valid Parentheses +Solution: +#CPP + +bool isValid(string s) { + stackst; + for (int i = 0; i < s.length(); i++) { + if (st.empty() || s[i] == '(' || s[i] == '{' || s[i] == '[') st.push(s[i]); + else { + if (s[i] == ')' && st.top() == '(') st.pop(); + else if (s[i] == '}' && st.top() == '{') st.pop(); + else if (s[i] == ']' && st.top() == '[') st.pop(); + else st.push(s[i]); + } + } + return st.size() == 0; +} \ No newline at end of file