Skip to content

Commit

Permalink
Merge pull request vikasgond807#12 from Aditi2905/main
Browse files Browse the repository at this point in the history
Valid parentheses code
  • Loading branch information
vikasgond807 committed Oct 12, 2022
2 parents 4d64d46 + 30f4234 commit a55c4f4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Valid_parentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <bits/stdc++.h>
using namespace std;


bool isValid(string x)
{
stack <char> st;
for(int i=0;i<x.length();i++)
{
if(st.empty()) st.push(x[i]);
else
{
if(x[i]=='(' || x[i]=='{' || x[i]=='[') st.push(x[i]);
else if(x[i]==']' && st.top()=='[') st.pop();
else if(x[i]=='}' && st.top()=='{') st.pop();
else if(x[i]==')' && st.top()=='(') st.pop();
else return false;
}
}
if(st.empty()) return true;
return false;
}


int main()
{
string s="{()}[]";
if(isValid(s)) cout<<"Valid";
else cout<<"Invalid";
}
Binary file added a.exe
Binary file not shown.

0 comments on commit a55c4f4

Please sign in to comment.