Skip to content

Practice Count K-Subsequences of a String With Maximum Beauty

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2842
LeetCode Problem

Count K-Subsequences of a String With Maximum Beauty

Problem Statement

For each character c, let f(c) be its frequency in the entire string s. A valid subsequence chooses k positions in increasing order and contains k distinct characters. Its beauty is the sum of f(c) over those characters. Count valid subsequences with the greatest possible beauty, modulo 10^9 + 7. Different position choices count separately even when they spell the same text; return 0 if no valid subsequence exists.

Source: Count K-Subsequences of a String With Maximum Beauty on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "bcca", k = 2
Output
4

# Example 2

Input
s = "abbcd", k = 4
Output
2

Constraints

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

Related LeetCode Problems

Practice more problems similar to Count K-Subsequences of a String With Maximum Beauty.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.