C++:
class Solution {
public:
int findTheLongestBalancedSubstring(string s) {
int n=s.size(),res=0;
for(int i=0;i<n;){
int a=0,b=0;
while(i<n&&s[i]=='0') {a++;i++;}
while(i<n&&s[i]=='1') {b++;i++;}
res=max(res,min(a,b)*2);
}
return res;
}
};
Python:
class Solution:
def findTheLongestBalancedSubstring(self, s: str) -> int:
n=len(s)
i=0
res=0
while i<n:
a=0
b=0
while i<n and s[i] =='0':
a+=1
i+=1
while i<n and s[i] =='1':
b+=1
i+=1
res=max(res,min(a,b)*2)
return res