Count K-Subsequences of a String With Maximum Beauty - Problem

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

example_1.py — Basic case
$ Input: s = "abbbdd", k = 2
Output: 1
💡 Note: Character frequencies: f('a')=1, f('b')=3, f('d')=2. Possible 2-subsequences: 'ab' (beauty=4), 'ad' (beauty=3), 'bd' (beauty=5). Maximum beauty is 5, achieved by 1 subsequence.
example_2.py — Multiple optimal solutions
$ Input: s = "aabbcc", k = 2
Output: 3
💡 Note: All characters have frequency 2. All 2-subsequences ('ab', 'ac', 'bc') have beauty 4. So 3 subsequences achieve maximum beauty.
example_3.py — Insufficient unique characters
$ Input: s = "aaa", k = 2
Output: 0
💡 Note: Only 1 unique character 'a', but we need k=2 unique characters. No valid k-subsequence exists.

Visualization

Tap to expand
The Beauty Contest: Optimal Selection StrategyAPopularity: 1BPopularity: 3DPopularity: 2Ranking by Popularity🥇 B (popularity: 3)🥈 D (popularity: 2)🥉 A (popularity: 1)Select top k=2: B + D🎯 Key Insight: Greedy Selection is OptimalTo maximize total beauty, always choose the k most frequent characters.Why? If we swap a more frequent char with a less frequent one, beauty decreases.When there are ties in frequency, use combinatorics to count all optimal selections.🔢 Combinatorics ExampleIf frequencies are [2,2,2] and k=2:• All have same frequency• Need to choose 2 from 3• Answer: C(3,2) = 3 ways• All achieve same max beauty!⚡ Complexity ComparisonBrute Force: O(C(n,k) × k) - exponentialOptimal: O(n + m log m) - much faster!n = string length, m = unique charactersUses math instead of enumeration 🚀
Understanding the Visualization
1
Talent Assessment
Count how popular each performer type is (character frequency)
2
Ranking
Rank all performer types by their popularity in descending order
3
Greedy Selection
Always pick the top k most popular types for maximum total talent
4
Handle Ties
When performers have equal popularity, use combinatorics to count selection ways
Key Takeaway
🎯 Key Insight: The optimal k-subsequence always contains the k most frequent characters. Use combinatorics to count arrangements when frequencies tie, achieving O(n + m log m) complexity instead of exponential brute force.

Time & Space Complexity

Time Complexity
⏱️
O(n + m log m)

O(n) to count frequencies, O(m log m) to sort m unique characters

n
2n
Linearithmic
Space Complexity
O(m)

O(m) space for frequency map where m is number of unique characters

n
2n
Linear Space

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
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
28.5K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen