1963. Minimum Number of Swaps to Make the String Balanced

PHOTO EMBED

Fri Feb 24 2023 12:20:11 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

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;
    }
};
content_copyCOPY

https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/