Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions BalancedParenthesis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<bits/stdc++.h>
using namespace std;
bool isBalanced(string s,int n)
{
stack<char> st;
char c;
for(int i=0;i<n;i++)
{
if((s[i]=='(')||(s[i]=='{')||(s[i]=='['))
{
st.push(s[i]);
continue;
}
if(st.empty())
return false;
switch(s[i])
{
case ')':c=st.top();
st.pop();
if(c!='(')
return false;
break;
case '}':c=st.top();
st.pop();
if(c!='{')
return false;
break;
case ']':c=st.top();
st.pop();
if(c!='[')
return false;
break;
}
}
if(st.empty())
return true;
return false;
}
int main()
{
string s;
cin>>s;
if(isBalanced(s,s.size()))
cout<<"Balanced paranthesis"<<endl;
else
cout<<"Not Balanced paranthesis"<<endl;
return 0;
}