Number of Unique Good Subsequences - Problem
Number of Unique Good Subsequences
You're given a binary string
A subsequence is good if:
• It's not empty
• It has no leading zeros (except for the single character "0")
For example, with
• 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
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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code