Created
July 17, 2017 22:53
-
-
Save na5imuzzaman/9794a5d205b352fbadb2dfa13cbd427e to your computer and use it in GitHub Desktop.
673 - Parentheses Balance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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