Imagine you're a talent scout looking for the most beautiful teams of unique performers from a group of artists. Each artist type appears multiple times, and a team's beauty is determined by how popular each artist type is!
Given a string s and an integer k, you need to find all possible k-subsequences with maximum beauty.
What's a k-subsequence?
A subsequence of length k where all characters are unique (no duplicates allowed).
What's beauty?
For each character c in the subsequence, let f(c) be how many times c appears in the original string s. The beauty is the sum of all f(c) values.
Example: s = "abbbdd", k = 2
• f('a') = 1, f('b') = 3, f('d') = 2
• Subsequence "ab" has beauty = 1 + 3 = 4
• Subsequence "ad" has beauty = 1 + 2 = 3
• Subsequence "bd" has beauty = 3 + 2 = 5 ← Maximum beauty!
Goal: Count how many k-subsequences achieve the maximum possible beauty. Return the answer modulo 109 + 7.
Input & Output
Visualization
Time & Space Complexity
O(n) to count frequencies, O(m log m) to sort m unique characters
O(m) space for frequency map where m is number of unique characters
Constraints
- 1 ≤ s.length ≤ 2 × 104
- 1 ≤ k ≤ s.length
- s consists of only lowercase English letters
- Answer fits in a 32-bit integer after taking modulo