Count K-Subsequences of a String With Maximum Beauty Problem
Description
LeetCode Problem 2842.
You are given a string s and an integer k.
A k-subsequence is a subsequence of s, having length k, and all its characters are 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.
For example, consider s = "abbbdd" and k = 2:
f('a') = 1,f('b') = 3,f('d') = 2- Some k-subsequences of
sare:"**ab**bbdd"->"ab"having a beauty off('a') + f('b') = 4"**a**bbb**d**d"->"ad"having a beauty off('a') + f('d') = 3"a**b**bb**d**d"->"bd"having a beauty off('b') + f('d') = 5
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 charactercoccurs ins, not 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:
Input: s = "bcca", k = 2
Output: 4
Explanation: From s we have f('a') = 1, f('b') = 1, and f('c') = 2.
The k-subsequences of s are:
bcca having a beauty of f('b') + f('c') = 3
bcca having a beauty of f('b') + f('c') = 3
bcca having a beauty of f('b') + f('a') = 2
bcca having a beauty of f('c') + f('a') = 3
bcca having a beauty of f('c') + f('a') = 3
There are 4 k-subsequences that have the maximum beauty, 3.
Hence, the answer is 4.
Example 2:
Input: s = "abbcd", k = 4
Output: 2
Explanation: From s we have f('a') = 1, f('b') = 2, f('c') = 1, and f('d') = 1.
The k-subsequences of s are:
abbcd having a beauty of f('a') + f('b') + f('c') + f('d') = 5
abbcd having a beauty of f('a') + f('b') + f('c') + f('d') = 5
There are 2 k-subsequences that have the maximum beauty, 5.
Hence, the answer is 2.
Constraints:
1 <= s.length <= 2 * 10^51 <= k <= s.lengthsconsists only of lowercase English letters.
Solution
Approach: Greedy + Combinatorics
Count how often each letter occurs. A k-subsequence uses k distinct letters, so if fewer than k distinct letters exist the answer is 0.
The beauty is the sum of the occurrence counts of the chosen letters, so we want the k largest counts. Sort the counts in descending order and let val be the k-th one; suppose x letters have count exactly val.
- Every letter with a count larger than
valmust be taken, and each can be picked in that many ways (one per occurrence). Multiply those counts together and reducekfor each. - From the
xletters tied atval, we choose the remainingkof them:C(x, k)ways, and each chosen letter hasvaloccurrences to pick from:val^k.
The answer is the product of all three parts modulo 10^9 + 7, using fast exponentiation for val^k.
Complexity: Time O(n), space O(1) for the 26 letters.
from collections import Counter
from math import comb
class Solution:
def countKSubsequencesWithMaxBeauty(self, s: str, k: int) -> int:
f = Counter(s)
if len(f) < k:
return 0
mod = 10**9 + 7
vs = sorted(f.values(), reverse=True)
val = vs[k - 1]
x = vs.count(val)
ans = 1
for v in vs:
if v == val:
break
k -= 1
ans = ans * v % mod
ans = ans * comb(x, k) * pow(val, k, mod) % mod
return ans