问:
在一个 平衡字符串 中,‘L’ 和 ‘R’ 字符的数量是相同的。
给你一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。
注意:分割得到的每个字符串都必须是平衡字符串。
返回可以通过分割得到的平衡字符串的 最大数量 。
原题链接:https://leetcode.cn/problems/split-a-string-in-balanced-strings/
例:
输入:s = “RLRRLLRLRL”
输出:4
答:
class Solution {
public int balancedStringSplit(String s) {
int a = 0;
int b = 0;
for(int i = 0;i < s.length();i++)
{
char c = s.charAt(i);
if(c == 'L')
b++;
else
b--;
if(b == 0)
a++;
}
return a;
}
}