Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Find the Longest Balanced Substring of a Binary String

Updated
2 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2609" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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'

Explanation

Here's an efficient solution to find the length of the longest balanced substring in a binary string:

  • Count Leading Zeroes and Trailing Ones: Iterate through the string to find the number of leading zeroes and trailing ones.
  • Determine the Minimum: The length of the longest balanced substring is twice the minimum of the counts of zeroes and ones. This is because a balanced substring must have an equal number of zeroes and ones, with all zeroes preceding all ones.
  • Handle Edge Cases: If no zeroes or no ones exist, the longest balanced substring has length 0 (excluding the empty substring).

  • Runtime Complexity: O(n), where n is the length of the string. Storage Complexity: O(1).

Code

    def longest_balanced_substring(s: str) -> int:
    """
    Finds the length of the longest balanced substring of a binary string.

    A substring is balanced if all zeroes are before ones and the number of
    zeroes is equal to the number of ones inside the substring.
    The empty substring is considered a balanced substring.

    Args:
        s: The binary string.

    Returns:
        The length of the longest balanced substring.
    """

    n = len(s)
    zero_count = 0
    one_count = 0

    # Count leading zeroes
    for char in s:
        if char == '0':
            zero_count += 1
        else:
            break

    # Count trailing ones
    for char in reversed(s):
        if char == '1':
            one_count += 1
        else:
            break

    return 2 * min(zero_count, one_count)

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Find the Longest Balanced Substring of a Binary String