fork download
  1. #include<iostream>
  2. #include<stack>
  3. #include<string>
  4. using namespace std;
  5.  
  6. bool balancedParanthesis(string s){
  7. stack<char>s1,s2,s3;
  8. for(int i=0;i<s.size();i++){
  9. char curChar = s[i];
  10. if(curChar=='('){
  11. s1.push(curChar);
  12. }
  13. else if(curChar==')'){
  14. if(s1.empty() || s1.top()!='('){
  15. return false;
  16. }
  17. s1.pop();
  18. }
  19.  
  20. if(curChar=='['){
  21. s2.push(curChar);
  22. }
  23. else if(curChar==']'){
  24. if(s2.empty() || s2.top()!='['){
  25. return false;
  26. }
  27. s2.pop();
  28. }
  29. if(curChar=='{'){
  30. s3.push(curChar);
  31. }
  32. else if(curChar=='}'){
  33. if(s3.empty() || s3.top()!='{'){
  34. return false;
  35. }
  36. s3.pop();
  37. }
  38.  
  39. }
  40. return s1.empty()&&s2.empty()&&s3.empty();
  41. }
  42.  
  43. int main(){
  44. string expr;
  45. cin>>expr;
  46. if(balancedParanthesis(expr)==true){
  47. cout<<"Yes";
  48. }
  49. else{
  50. cout<<"No";
  51. }
  52. return 0;}
  53.  
Success #stdin #stdout 0s 4356KB
stdin
Standard input is empty
stdout
Yes