1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public int findTheLongestBalancedSubstring(String s) { int ans = 0, n = s.length(), k = 0; for (int i = 0; i < n; ) { int cnt0 = 0, cnt1 = 0; while (k < n && s.charAt(k) == '0') { ++k; ++cnt0; } while (k < n && s.charAt(k) == '1') { ++k; ++cnt1; } ans = Math.max(ans, Math.min(cnt0, cnt1) * 2); i = k; } return ans; } }
|