2609. Find the Longest Balanced Substring of a Binary String LeetCode Solution

In this guide, you will get 2609. Find the Longest Balanced Substring of a Binary String LeetCode Solution with the best time and space complexity. The solution to Find the Longest Balanced Substring of a Binary String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Longest Balanced Substring of a Binary String solution in C++
  4. Find the Longest Balanced Substring of a Binary String solution in Java
  5. Find the Longest Balanced Substring of a Binary String solution in Python
  6. Additional Resources
2609. Find the Longest Balanced Substring of a Binary String LeetCode Solution image

Problem Statement of Find the Longest Balanced Substring of a Binary String

You are given a binary string s consisting only of zeroes and ones.
A substring of s is considered balanced if all zeroes are before ones and the number of zeroes is equal to the number of ones inside the substring. Notice that the empty substring is considered a balanced substring.
Return the length of the longest balanced substring of s.
A substring is a contiguous sequence of characters within a string.

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.

Constraints:

1 <= s.length <= 50
'0' <= s[i] <= '1'

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2609. Find the Longest Balanced Substring of a Binary String LeetCode Solution in C++

class Solution {
 public:
  int findTheLongestBalancedSubstring(string s) {
    int ans = 0;

    for (int i = 0; i < s.length();) {
      int zeros = 0;
      int ones = 0;
      while (i < s.length() && s[i] == '0') {
        ++zeros;
        ++i;
      }
      while (i < s.length() && s[i] == '1') {
        ++ones;
        ++i;
      }
      ans = max(ans, min(zeros, ones));
    }

    return ans * 2;
  }
};
/* code provided by PROGIEZ */

2609. Find the Longest Balanced Substring of a Binary String LeetCode Solution in Java

class Solution {
  public int findTheLongestBalancedSubstring(String s) {
    int ans = 0;

    for (int i = 0; i < s.length();) {
      int zeros = 0;
      int ones = 0;
      while (i < s.length() && s.charAt(i) == '0') {
        ++zeros;
        ++i;
      }
      while (i < s.length() && s.charAt(i) == '1') {
        ++ones;
        ++i;
      }
      ans = Math.max(ans, Math.min(zeros, ones));
    }

    return ans * 2;
  }
}
// code provided by PROGIEZ

2609. Find the Longest Balanced Substring of a Binary String LeetCode Solution in Python

class Solution:
  def findTheLongestBalancedSubstring(self, s: str) -> int:
    ans = 0

    i = 0
    while i < len(s):
      zeros = 0
      ones = 0
      while i < len(s) and s[i] == '0':
        zeros += 1
        i += 1
      while i < len(s) and s[i] == '1':
        ones += 1
        i += 1
      ans = max(ans, min(zeros, ones))

    return ans * 2
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.