Skip to content

Instantly share code, notes, and snippets.

@na5imuzzaman
Created July 17, 2017 22:53
Show Gist options
  • Save na5imuzzaman/9794a5d205b352fbadb2dfa13cbd427e to your computer and use it in GitHub Desktop.
Save na5imuzzaman/9794a5d205b352fbadb2dfa13cbd427e to your computer and use it in GitHub Desktop.
673 - Parentheses Balance
/* Nasim */
#include<bits/stdc++.h>
#include<iostream>
#include<stack>
using namespace std;
void Balance(char *s,int n)
{
stack <char> c;
for (int i=0; i<n; i++)
{
if (s[i] == '(' || s[i] == '[')
{
c.push(s[i]);
}
else if (s[i] == ']' || s[i] == ')')
{
if (c.empty())
{
cout<<"No\n";
return;
}
else if(s[i] == ')')
{
if (c.top() == '(')
{
c.pop();
}
}
else if (s[i] == ']')
{
if (c.top() == '[')
{
c.pop();
}
}
else
{
cout<<"No\n";
return;
}
}
}
if (!c.empty())
cout<<"No\n";
else if (c.empty())
cout<<"Yes\n";
}
int main()
{
int x;
cin>>x;
getchar();
char n[129];
while (x--)
{
//scanf("%[^\n]s", n);
gets(n);
Balance(n,strlen(n));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment