2. Good Subsequences
A subsequence of a given string is generated by deleting zero or more characters from a string, then concatenating the remaining characters. A good subsequence is one where the frequency of each character is the same. Given a string that consists of n Latin letters, determine how many good subsequences it contains. Since the answer can be quite large, compute its modulo (10⁹ + 7).
Note: An empty subsequence is not a good subsequence.
Example
- word =
"abca"
A total of 15 non-empty subsequences can be formed from words:"a"
, "a"
, "aa"
, "ab"
, "aba"
, "abc"
, "abca"
, "ac"
, "aca"
, "b"
, "ba"
, "bc"
, "bca"
, "c"
, and "ca"
.
The only subsequences that are not good are "aba"
, "aca"
, and "abca"
as the frequency of character "a"
is 2, and every other character is 1.
The total number of good subsequences = 15 - 3 = 12 and the answer to the above example = 12 modulo (10⁹ + 7) = 12.
Function Description
Complete the function countGoodSubsequences in the editor below.
countGoodSubsequences has the following parameter(s):
- string word: a string that consists of only lowercase Latin letters.
Returns
- int: the number of good subsequences modulo (10⁹ + 7).
Constraints
- 1 ≤ length of word ≤ 10⁵
- word[i] is in the range [a-z]
Sample Input For Custom Testing
Sample Case 0
STDIN → FUNCTIONabcd
→ word = "abcd"
Sample Output
15
Explanation
All of the non-empty subsequences are good subsequences. They are:"a"
, "ab"
, "abc"
, "abcd"
, "abd"
, "ac"
, "acd"
, "ad"
, "b"
, "bc"
, "bcd"
, "bd"
, "c"
, "cd"
, and "d"
.
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.