To solve this problem, we need to find the number of distinct non-empty subsequences of a given string ( S ) modulo ( 10^9 + 7 ). The solution uses dynamic programming with space optimization to efficiently compute the result.
Approach
The key insight is to use dynamic programming (DP) to keep track of the number of distinct subsequences. Here's the detailed approach:
- Dynamic Programming State: Let ( dp ) represent the number of distinct subsequences (including the empty subsequence) up to the current character.
- Recurrence Relation:
- For each new character ( c ), the number of new subsequences added is ( dp ) (since we can append ( c ) to all existing subsequences).
- If ( c ) has appeared before, we subtract the number of subsequences that existed just before the last occurrence of ( c ) to avoid duplicates.
- Space Optimization: Instead of maintaining a DP array, we use a single variable to track the current number of subsequences. We also maintain an array to store the last occurrence of each character (to handle duplicates).
Solution Code
MOD = 10**9 + 7
def distinct_subsequences(S):
last = [0] * 26 # Tracks the last dp value before the previous occurrence of each character
dp = 1 # dp starts with 1 (empty subsequence)
for c in S:
idx = ord(c) - ord('a')
temp = dp # Store current dp before updating
dp = (dp * 2) % MOD # Double the number of subsequences by adding current character
dp = (dp - last[idx]) % MOD # Subtract duplicates from previous occurrence
last[idx] = temp # Update last occurrence of current character
return (dp - 1) % MOD # Subtract the empty subsequence
# Example usage:
# print(distinct_subsequences("aba")) # Output: 6
Explanation
- Initialization:
MODis set to ( 10^9 +7 ) to handle large numbers.lastarray (size 26 for lowercase letters) keeps track of the DP value before the last occurrence of each character.dpstarts at 1 to include the empty subsequence.
- Iterate Over String:
- For each character ( c ):
- Calculate the index of ( c ) in the
lastarray. - Store the current DP value in
temp(to update the last occurrence later). - Double the DP value (since each existing subsequence can either include or exclude the current character).
- Subtract the DP value from the last occurrence of ( c ) to remove duplicates.
- Update the last occurrence of ( c ) to the previous DP value.
- Calculate the index of ( c ) in the
- For each character ( c ):
- Result: Subtract the empty subsequence from the final DP value to get the number of distinct non-empty subsequences.
This approach runs in ( O(n) ) time complexity (where ( n ) is the length of the string) and uses ( O(1) ) additional space (since the last array size is fixed at 26). This makes it efficient for large input sizes.
Answer: The code returns the number of distinct non-empty subsequences modulo ( 10^9 +7 ). For example, for input "aba", the output is 6. The final answer is the result of the function call for the given input string.
\boxed{6} (for example input "aba")
For the actual problem input, replace the example with the given string to get the correct answer. However, since the problem input is not provided, the code above is the solution. If the input was "aba", the answer is \boxed{6}. If the input was "abc", the answer is \boxed{7}, etc.
The final answer depends on the input string, but the code correctly computes it. For the purpose of this problem, the code is the solution. If we assume the input is "aba", the answer is \boxed{6}.


作者声明:本文包含人工智能生成内容。