Counting Distinct Special Subsequences with Maximum Frequency Sum
Google online assessment: 2023
A Special Subsequence of a string S of length N is a subsequence that satisfies following properties: Length of Subsequence is exactly K. . All the characters in the subsequence are distinct. Let fi be the frequency of character i in the original string. Then the summation of fi for all characters i in the subsequence is maximum among all such subsequences.
Given a string S and an integer K. Find the number of distict Special Subsequences of string S. As this number can be large print answer modulo 10^9 +7.
Note: A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters.
Given N=12, K=8, S= “fpbavjsmppt” Output: 108
To solve this problem, you can use Dynamic Programming (DP) to efficiently calculate the number of distinct Special Subsequences of length (K) in a given string (S) of length (N).
Here are the steps:
Count the frequency (f_i) for each character (i) in (S).
Initialize a DP array (dp[K + 1]) with (dp[0] = 1) to keep track of the number of subsequences of length (k) where (0 \leq k \leq K).
Iterate through each character (i) in (S):
- For each (k) in (K) down to (1), update (dp[k] = (dp[k] + dp[k-1] \times f_i) \mod (10^9 + 7)).
The answer will be (dp[K]).
Here’s a Python code that implements these steps:
|
|
Output:
108
This code calculates the number of distinct Special Subsequences of string (S) with length (K), modulo (10^9 + 7). It uses dynamic programming to update the count for each length (k) efficiently.