#2609
Find the Longest Balanced Substring of a Binary String
pupil · 465 · lc easy +26 · verified · 46.4% accepted · 392 likes · top 31%
Description
You are given a binary string s of '0's and '1's. A balanced substring has all zeros before all ones with equal counts of each; the empty substring also qualifies. Return the length of the longest balanced substring of s.
Example 1:
Input: s = "01000111"
Output: 6
Explanation: The longest balanced substring is "000111", which has length 6.
Example 2:
Input: s = "00111"
Output: 4
Explanation: The longest balanced substring is "0011", which has length 4.
Example 3:
Input: s = "111"
Output: 0
Explanation: There is no balanced substring except the empty substring, so the answer is 0.
Code
1
2
3