Advertisement
Adrita

checking braces using stack

Feb 1st, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. string a;
  6. cin>>a;
  7. stack <char> s;
  8. for(int i=0; i<a.size(); i++)
  9. {
  10. if(a[i]=='('||a[i]=='{'||a[i]=='[')
  11. {
  12. s.push(a[i]);
  13. }
  14. else if(a[i]==')'||a[i]=='}'||a[i]==']')
  15. {
  16. if(s.empty()||(a[i]==')'&&s.top()!='(')||(a[i]=='}'&&s.top()!='{')||(a[i]==']'&&s.top()!='['))
  17. {
  18. cout<<"NO";
  19. return 0;
  20. }
  21. else
  22. s.pop();
  23. }
  24. }
  25. if(s.empty())
  26. cout<<"YES";
  27. else cout<<"NO";
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement