Last active
July 8, 2022 10:29
-
-
Save KhaledElshamy/cc04ca0b1d261b0bb940d2cf74aba9de to your computer and use it in GitHub Desktop.
Problem: Check brackets in the code (Data Structures course at coursera)
This file contains hidden or 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
| #include <bits/stdc++.h> | |
| using namespace std; | |
| int main() | |
| { | |
| string str1; | |
| int index=0,balance=true,a; | |
| char top; | |
| cin>>str1; | |
| stack<char>s1; | |
| while(index<str1.length()) | |
| { | |
| if (s1.empty()) | |
| a=index; | |
| if(str1[index]=='['||str1[index]=='{'||str1[index]=='(') | |
| s1.push(str1[index]); | |
| else | |
| { | |
| if((str1[index]==']'||str1[index]=='}'||str1[index]==')')) | |
| { | |
| if(s1.empty()) | |
| { | |
| balance=false; | |
| break; | |
| } | |
| else | |
| { | |
| top=s1.top(); | |
| s1.pop(); | |
| if(( top =='[' && str1[index]!=']')||( top=='{' && str1[index]!='}')||( top=='(' && str1[index]!=')')) | |
| { | |
| balance=false; | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| index+=1; | |
| } | |
| if(balance&&s1.empty()) | |
| cout<<"Success"<<endl; | |
| else | |
| { | |
| if((index>=str1.size())&&!s1.empty()) | |
| cout<<a+1<<endl; | |
| else | |
| cout<<index+1<<endl; | |
| } | |
| return 0; | |
| } |
SachinManras
commented
Jul 8, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment