Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Number of Unique Good Subsequences

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1987" 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 binary. A subsequence of binary is considered good if it is not empty and has no leading zeros (with the exception of "0"). Find the number of unique good subsequences of binary. For example, if binary = "001", then all the good subsequences are ["0", "0", "1"], so the unique good subsequences are "0" and "1". Note that subsequences "00", "01", and "001" are not good because they have leading zeros. Return the number of unique good subsequences of binary. Since the answer may be very large, return it modulo 109 + 7. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. Example 1: Input: binary = "001" Output: 2 Explanation: The good subsequences of binary are ["0", "0", "1"]. The unique good subsequences are "0" and "1". Example 2: Input: binary = "11" Output: 2 Explanation: The good subsequences of binary are ["1", "1", "11"]. The unique good subsequences are "1" and "11". Example 3: Input: binary = "101" Output: 5 Explanation: The good subsequences of binary are ["1", "0", "1", "10", "11", "101"]. The unique good subsequences are "0", "1", "10", "11", and "101". Constraints: 1 <= binary.length <= 105 binary consists of only '0's and '1's.

Explanation

Here's the breakdown of the solution:

  • Dynamic Programming: We use dynamic programming to count the number of unique subsequences ending with '0' and '1'.
  • Unique Counting: The key idea is to maintain counts for unique subsequences, ensuring that we don't double-count subsequences when encountering duplicate characters.
  • Modulo Arithmetic: We perform modulo operations throughout to prevent integer overflow.

  • Time Complexity: O(n), where n is the length of the binary string. Space Complexity: O(1).

Code

    def unique_good_subsequences(binary: str) -> int:
    """
    Finds the number of unique good subsequences of a binary string.

    Args:
        binary: The binary string.

    Returns:
        The number of unique good subsequences modulo 10^9 + 7.
    """

    MOD = 10**9 + 7
    ends_with_0 = 0
    ends_with_1 = 0
    has_zero = False

    for bit in binary:
        if bit == '0':
            ends_with_0 = (ends_with_0 + ends_with_1) % MOD
            has_zero = True
        else:
            ends_with_1 = (ends_with_1 + ends_with_1 + 1) % MOD

    if has_zero:
        return (ends_with_0 + ends_with_1 + 1) % MOD
    else:
        return ends_with_1

More from this blog

C

Chatmagic blog

2894 posts