class Solution { public: int minSwaps(string s) { int n=s.size(); stack<char> st; for(int i=0;i<n;i++) { if(!st.empty()&&st.top()=='['&&s[i]==']') { st.pop(); } else st.push(s[i]); } int n2=st.size()/2; if(n2%2==1) return (n2/2)+1; else return n2/2; } };
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