题目来源:
leetcode题目,网址:2609. 最长平衡子字符串 - 力扣(LeetCode)
解题思路:
按要求进行模拟即可。
解题代码:
class Solution {
public int findTheLongestBalancedSubstring(String s) {
int res=0;
int zero=0;
int one=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='0'){
if(one==0){
zero++;
}else{
zero=1;
one=0;
}
}else{
one++;
if(one<=zero){
res=Math.max(res,2*one);
}
}
}
return res;
}
}
总结:
注意,当 1 的数量小于等于 0 的数量,可以组成一个长度为 1 的数量两倍的平衡字符串。
无官方题解。