Count K-Subsequences of a String With Maximum Beauty

You are given a string s and an integer k.

A k-subsequence is a subsequence of s that has length k and whose characters are all unique, i.e., every character occurs once.

Let f(c) denote the number of times the character c occurs in s.

The beauty of a k-subsequence is the sum of f(c) for every character c in the k-subsequence.

Return an integer denoting the number of k-subsequences whose beauty is the maximum among all k-subsequences. Since the answer may be too large, return it modulo 10^9 + 7.

A subsequence of a string is a new string formed from the original string by deleting some, possibly none, of the characters without disturbing the relative positions of the remaining characters.

Notes

  • f(c) is the number of times a character c occurs in s, not in a k-subsequence.
  • Two k-subsequences are considered different if one is formed by an index that is not present in the other, so two k-subsequences may form the same string.
Example 1
Inputs = "bcca", k = 2
Output4
There are 4 k-subsequences that have the maximum beauty, 3.
Example 2
Inputs = "abbcd", k = 4
Output2
There are 2 k-subsequences that have the maximum beauty, 5.

Constraints

  • 1 <= s.length <= 2 * 10^5
  • 1 <= k <= s.length
  • s consists only of lowercase English letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate