1221 balance split string

Input string and the number of R L has the same
title and the balancing requirements are not required string, when each character string balancing the requirements of an application or a dual stack pointer traversal

class Solution {
public:
    int balancedStringSplit(string s) {
        int ans = 0, cnt = 0;
        for(int i = 0; i < s.size(); ++i)
        {
            if(s[i] == 'L') ++cnt;
            else --cnt;
            if(!cnt) ++ans;
        }
        return ans ? ans : 1;
    }
};
Published 21 original articles · won praise 32 · views 20000 +

Guess you like

Origin blog.csdn.net/include_not_found_/article/details/104896378