题目描述:
在一个「平衡字符串」中,‘L’ 和 ‘R’ 字符的数量是相同的。
给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。
返回可以通过分割得到的平衡字符串的最大数量。
思路分析:局部求最优解,用flag标志是否可以分割成一个平衡字符串,count是平衡字符串的数量
class Solution {
public int balancedStringSplit(String s) {
if(s==null){
return 0;
}
int count=0;
int flag=0;
for(char x:s.toCharArray()){
if(x=='R')
flag++;
else
flag--;
if(flag==0)
count++;
}
return count;
}
}