From c9a4221b136ba36a118a8392fe6bfff66b7f202a Mon Sep 17 00:00:00 2001 From: Kumar ayush <112839746+A-Y12@users.noreply.github.com> Date: Sat, 25 Feb 2023 23:36:26 +0530 Subject: [PATCH] Valid Parantheses --- valid parantheses | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 valid parantheses diff --git a/valid parantheses b/valid parantheses new file mode 100644 index 0000000..6f238f3 --- /dev/null +++ b/valid parantheses @@ -0,0 +1,47 @@ +class Solution { +public: + bool isValid(string s) + { + stack k; + char ch; + for(int i=0;i< s.length();i++) + { + ch=s[i]; + if(ch=='('|| ch=='{'||ch=='[') + { + k.push(ch); + } + else + { + if(!k.empty()) + { + if((ch==')'&& k.top()=='(')|| (ch=='}'&&k.top()=='{')||(ch==']'&&k.top()=='[')) + { + k.pop(); + } + else + { + return false; + } + } + else + { + return false; + } + } + + } + if(k.empty()) + { + return true; + } + else + { + return false; + } + + + + + } +};