1221. segmentation balance string
description:
In a "balanced string", the number of 'L' and 'R' character is the same.
Given a balanced string s, you divide it into as many balance strings.
Returns the maximum number of equilibrium can be obtained by a division of the string.
Example 1:
Input: s = "RLRRLLRLRL"
Output: 4
explains: S may be divided into "RL", "RRLL", "RL", "RL", each sub-string contains the same number of 'L' and 'R'.
Example 2:
Input: s = "RLLLLRRRLR"
Output: 3
explains: S may be divided into "RL", "LLLRRR", "LR", each sub-string contains the same number of 'L' and 'R'.
Example 3:
Input: s = "LLLLRRRR"
Output: 1
explains: S can remain as "LLLLRRRR".
Example 4:
Input: s = "RRLRRLRLLLRL"
Output: 2
prompt:
1 <= s.length <= 1000
s[i] = 'L' 或 'R'
1 Solution: 0 MS 8.4 MB 2 class Solution { . 3 public : . 4 int balancedStringSplit ( String S) { . 5 / * ideas: . 6 1: Traversal string, index = 0 starts, before the number of records R or L sumx, sumy ; . 7 2: current statistics L and R, once equal sum ++, sumx = 0, sumy = 0; can continue to the next counted . 8 * / . 9 int SUM = 0 ; 10 int SUMX = 0 , Sumy = 0 ; . 11 for ( int = I 0;i<s.size();i++){ 12 if(s[i]=='L') sumx++; 13 else sumy++; 14 if(sumx==sumy){ 15 sum++; 16 sumx=0,sumy=0; 17 } 18 } 19 return sum; 20 } 21 };