Number of Unique Good Subsequences - Problem
Number of Unique Good Subsequences

You're given a binary string binary containing only '0's and '1's. Your task is to find all unique good subsequences from this string.

A subsequence is good if:
• It's not empty
• It has no leading zeros (except for the single character "0")

For example, with binary = "001":
• All possible subsequences: "", "0", "0", "1", "00", "01", "01", "001"
• Good subsequences: "0", "0", "1" (removing empty and those with leading zeros)
Unique good subsequences: "0", "1"

Return the count of unique good subsequences modulo 109 + 7.

Input & Output

example_1.py — Basic Case
$ Input: binary = "001"
Output: 2
💡 Note: Good subsequences are: "0" (from index 0), "0" (from index 1), "1" (from index 2). Unique good subsequences are "0" and "1", so answer is 2.
example_2.py — Only Ones
$ Input: binary = "101"
Output: 5
💡 Note: Good subsequences are: "1" (index 0), "0", "1" (index 2), "10", "11", "01" (invalid - leading zero), "101" (invalid - leading zero). Unique good ones: "1", "0", "10", "11", "101" → Wait, "101" has leading zero from middle '0'. Correct unique good ones: "1", "0", "10", "11". Answer is 5: {"0", "1", "10", "11", "01"}. Actually, "01" is invalid, so we have {"0", "1", "10", "11", "101" without leading zeros} = 5.
example_3.py — All Ones
$ Input: binary = "11"
Output: 3
💡 Note: All subsequences: "1" (index 0), "1" (index 1), "11". No '0' exists, so no "0" subsequence. Unique good subsequences: "1", "11". Answer is 2. Wait, let me recalculate: we have "1", "1", "11" → unique are "1", "11" = 2 unique good subsequences.

Constraints

  • 1 ≤ binary.length ≤ 105
  • binary[i] is either '0' or '1'
  • Answer must be returned modulo 109 + 7

Visualization

Tap to expand
Binary Subsequence Factory: "001" → Unique Good ProductsProduction LinesEnding with '0': Count = dp0Ending with '1': Count = dp1Processing Steps:0Step 1: Process first '0'dp0 = 0 + 0 = 0, dp1 = 0 (no new valid codes yet)0Step 2: Process second '0'dp0 = 0 + 0 = 0, dp1 = 0 (still no valid extensions)1Step 3: Process '1'dp1 = 0 + 0 + 1 = 1 (creates new code "1")Valid Products Created:"1"✓ Valid (no leading zero)"0"✓ Special case (single '0')Total Unique: 2🎯 Key Insight: Mathematical CountingInstead of generating all subsequences, we count unique ones by trackinghow many end with '0' vs '1' - avoiding exponential time complexity!Final Result: dp0 + dp1 + hasZero = 0 + 1 + 1 = 2Time: O(n) | Space: O(1) - Optimal solution!
Understanding the Visualization
1
Initialize Counters
Start with two production lines: one for codes ending in '0', one for codes ending in '1'
2
Process Each Digit
For '1': create new codes by appending to all existing codes. For '0': only append to codes ending in '1' to avoid leading zeros
3
Track Uniqueness
The DP approach automatically handles uniqueness by counting states rather than generating actual strings
4
Handle Special Case
Add 1 to final count if the string contains '0' (for the single character "0" subsequence)
Key Takeaway
🎯 Key Insight: Use DP to count unique subsequences mathematically instead of generating them explicitly, avoiding exponential complexity while handling the leading zero constraint elegantly.
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
23.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen