LC 1221. Split a String in Balanced Strings

Nilanjan Deb · April 28, 2020

This is my CPP solution.

class Solution {
public:
    int balancedStringSplit(string s) {
        int cnt = 0;
        string stack;
        for(int i=0;i<s.size();i++){
            stack.push_back(s[i]);
            int n = stack.size();
            if(n>1 && stack[n-1]!=stack[n-2]){
                stack.pop_back();
                stack.pop_back();
            }
            if(stack.size() == 0){
                cnt++;
            }
        }
        return cnt;
    }
};


Dicussion Forum