class Solution { public: //Function to check if brackets are balanced or not. bool ispar(string x) { stack<char> s; int n=x.size(); for(int i=0;i<n;i++) { if( !s.empty()&&((s.top()=='[' && x[i]==']')||(s.top()=='{' && x[i]=='}')||(s.top() == '(' && x[i]==')' ))) { s.pop(); } else s.push(x[i]); } if(s.empty()) return true; else return false; // Your code here } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter