Number of Unique Good Subsequences - Complete Solution Guide
Number of Unique Good Subsequences is LeetCode problem 1987, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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 . S
Detailed Explanation
The problem asks us to find the number of unique good subsequences of a given binary string. A subsequence is derived from a string by deleting some or no characters without changing the order of the remaining characters. A 'good' subsequence must not be empty and should not have leading zeros, with the exception of the subsequence '0' itself. The goal is to count the number of *unique* such good subsequences, modulo 10^9 + 7.
Solution Approach
The solution uses dynamic programming with two variables: `ends_with_0` and `ends_with_1`. These variables keep track of the number of unique good subsequences ending with '0' and '1', respectively. The algorithm iterates through the binary string, updating these variables based on the current digit. If the digit is '1', the number of subsequences ending with '1' becomes the sum of the previous number of subsequences ending with '0', '1', and the '1' itself (representing a new subsequence consisting only of '1'). If the digit is '0', the number of subsequences ending with '0' becomes the sum of the previous number of subsequences ending with '0' and '1'. Finally, a boolean variable `has_zero` is used to indicate if the string contains a zero. If so, we add 1 to the final count to account for the single '0' subsequence. The modulo operation is applied after each calculation to prevent integer overflow.
Step-by-Step Algorithm
- Step 1: Initialize `ends_with_0` and `ends_with_1` to 0. These represent the number of unique good subsequences ending with '0' and '1' respectively.
- Step 2: Initialize `has_zero` to 0 (or false). This flag will be set to 1 (or true) if the input string contains at least one '0'.
- Step 3: Iterate through the input binary string, character by character.
- Step 4: If the current digit is '1', update `ends_with_1` to `(ends_with_0 + ends_with_1 + 1) % MOD`. This accounts for adding '1' to all existing subsequences (ending in '0' or '1') and adding the single '1' subsequence.
- Step 5: If the current digit is '0', update `ends_with_0` to `(ends_with_0 + ends_with_1) % MOD`. This accounts for adding '0' to all existing subsequences (ending in '0' or '1'). Also, set `has_zero` to 1.
- Step 6: After iterating through the entire string, the final answer is `(ends_with_0 + ends_with_1 + has_zero) % MOD`. This is the sum of all subsequences ending with '0' and '1', plus 1 if the string contained a '0' (to account for the lone '0' subsequence).
Key Insights
- Insight 1: The core idea is to use dynamic programming to track the number of subsequences ending with '0' and '1'. This helps avoid generating and storing all possible subsequences, which would be inefficient.
- Insight 2: We only need to keep track of the number of subsequences ending with '0' and '1' at each step because adding a '1' to subsequences ending with '0' or '1' always results in a valid subsequence, and similarly, adding a '0' to existing subsequences ending with '0' or '1' results in valid ones. The 'unique' constraint is implicitly handled.
- Insight 3: The edge case of the single '0' subsequence needs special consideration. We use a `has_zero` flag to track whether the original string contains a zero and add it to the final count if it does.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: String, Dynamic Programming.
Companies
Asked at: Oracle.