2609. Find the Longest Balanced Substring of a Binary String

```java
class Solution {
    public int findTheLongestBalancedSubstring(String s) {
        int result = 0;
        for (int i = 0; i < s.length()-1; i++) {
            if (s.charAt(i) == '0' && s.charAt(i+1) == '1') {
                result = Math.max(result, calResult(i-1, i+2, s)+2);
            }
        }
        return result;
    }
    private int calResult(int i, int j, String s) {
        int result = 0;
        while (i >= 0 && j < s.length() && s.charAt(i) == '0' && s.charAt(j) == '1') {
            result +=2;
            i--;
            j++;
        }
        return result;
    }
}

/*
T :O(n)
S: O(1)
using sliding window? -> no

01000111

find 01 first

find index 01
then index0 -1 == 0 && index1 +1 == 1. -> +2 (while loop)

*/
```

Last updated