Advertisement
Rimifawfaw

matching brackets

Sep 24th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n,m=0,k=0,i,j;
  7. stack<char>a,b,c;
  8. string s;
  9. cin>>s;
  10. if(s[0]==')'||s[0]=='}'||s[0]==']'){cout<<"No"<<endl;return 0;}
  11. for(i=0;i<s.length();i++)
  12. {
  13.  
  14. if(s[i]=='('||s[i]=='{'||s[i]=='[')
  15. {a.push(s[i]);
  16. m++;//cout<<s[i]<<endl;
  17. }
  18. else {
  19. if(a.size()==0 && (s[i]==')'||s[i]=='}'||s[i]==']'))
  20. {
  21. cout<<"No"<<endl;
  22. return 0;
  23. }
  24.  
  25. if(s[i]==')'&& a.top()=='(')
  26. {
  27. a.pop();
  28.  
  29.  
  30. }
  31. if(s[i]=='}'&&a.top()=='{')
  32. {
  33. a.pop();
  34.  
  35. }
  36. if(s[i]==']'&&a.top()=='[')
  37. {
  38. a.pop();
  39. }
  40.  
  41. }
  42. }
  43. if(a.empty()==0)
  44. {
  45. cout<<"No"<<endl;
  46. }
  47. else
  48. {
  49. cout<<"Yes"<<endl;
  50.  
  51. }
  52.  
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement