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 s are:
"_**ab**_ bbdd" -> "ab" having a beauty of f('a') + f('b') = 4
"_**a**_ bbb** _d_** d" -> "ad" having a beauty of f('a') + f('d') = 3
"a** _b_** bb _**d**_ d" -> "bd" having a beauty of f('b') + f('d') = 5
Return an integer denoting the number of k-subsequenceswhosebeauty 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 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.
Input: s ="bcca", k =2Output: 4Explanation: From s we have f('a')=1, f('b')=1, and f('c')=2.The k-subsequences of s are:**_bc_** ca having a beauty of f('b')+ f('c')=3**_b_** c _**c**_ a having a beauty of f('b')+ f('c')=3**_b_** cc** _a_** having a beauty of f('b')+ f('a')=2b** _c_** c _**a**_**** having a beauty of f('c')+ f('a')=3bc** _ca_** having a beauty of f('c')+ f('a')=3There are 4 k-subsequences that have the maximum beauty,3.Hence, the answer is4.
Input: s ="abbcd", k =4Output: 2Explanation: From s we have f('a')=1, f('b')=2, f('c')=1, and f('d')=1.The k-subsequences of s are:_**ab**_ b** _cd_** having a beauty of f('a')+ f('b')+ f('c')+ f('d')=5_**a**_ b _**bcd**_ having a beauty of f('a')+ f('b')+ f('c')+ f('d')=5There are 2 k-subsequences that have the maximum beauty,5.Hence, the answer is2.
To maximize the beauty of a k-subsequence, we should pick the k most frequent unique characters. The more frequent a character, the more ways it can be chosen in a subsequence, and the higher the beauty sum. The number of such subsequences is determined by the product of the frequencies (as each character can be picked from its occurrences in the string).
classSolution {
public:int countKSubsequencesWithMaxBeauty(string s, int k) {
constint MOD =1e9+7;
unordered_map<char, int> freq;
for (char c : s) freq[c]++;
vector<int> f;
for (auto& p : freq) f.push_back(p.second);
if ((int)f.size() < k) return0;
sort(f.rbegin(), f.rend());
int kth = f[k-1], cnt =0, total =0;
for (int x : f) if (x == kth) cnt++;
for (int i =0; i < k; ++i) if (f[i] == kth) total++;
longlong ans =1;
for (int i =0; i < k; ++i) ans = ans * f[i] % MOD;
// C(cnt, total)
for (int i =1; i <= total; ++i) ans = ans * (cnt - i +1) % MOD;
for (int i =1; i <= total; ++i) ans = ans * powmod(i, MOD-2, MOD) % MOD;
return (int)ans;
}
longlongpowmod(longlong a, longlong b, longlong m) {
longlong r =1;
while (b) {
if (b &1) r = r * a % m;
a = a * a % m;
b >>=1;
}
return r;
}
};
classSolution {
staticfinalint MOD = 1000000007;
publicintcountKSubsequencesWithMaxBeauty(String s, int k) {
Map<Character, Integer> freq =new HashMap<>();
for (char c : s.toCharArray()) freq.put(c, freq.getOrDefault(c, 0) + 1);
List<Integer> f =new ArrayList<>(freq.values());
if (f.size() < k) return 0;
f.sort(Collections.reverseOrder());
int kth = f.get(k-1), cnt = 0, total = 0;
for (int x : f) if (x == kth) cnt++;
for (int i = 0; i < k; ++i) if (f.get(i) == kth) total++;
long ans = 1;
for (int i = 0; i < k; ++i) ans = ans * f.get(i) % MOD;
for (int i = 1; i <= total; ++i) ans = ans * (cnt - i + 1) % MOD;
for (int i = 1; i <= total; ++i) ans = ans * powmod(i, MOD-2, MOD) % MOD;
return (int)ans;
}
longpowmod(long a, long b, long m) {
long r = 1;
while (b > 0) {
if ((b & 1) == 1) r = r * a % m;
a = a * a % m;
b >>= 1;
}
return r;
}
}
classSolution {
privateval MOD = 1_000_000_007L
funcountKSubsequencesWithMaxBeauty(s: String, k: Int): Int {
val freq = s.groupingBy { it }.eachCount().values.toList()
if (freq.size < k) return0val f = freq.sortedDescending()
val kth = f[k-1]
val cnt = f.count { it== kth }
val total = f.take(k).count { it== kth }
var ans = 1Lfor (i in0 until k) ans = ans * f[i] % MOD
for (i in1..total) ans = ans * (cnt-i+1) % MOD
for (i in1..total) ans = ans * powmod(i.toLong(), MOD-2, MOD) % MOD
return ans.toInt()
}
privatefunpowmod(a: Long, b: Long, m: Long): Long {
var res = 1L; var aa = a; var bb = b
while (bb > 0) {
if (bb and 1L==1L) res = res * aa % m
aa = aa * aa % m
bb = bb shr 1 }
return res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defcountKSubsequencesWithMaxBeauty(self, s: str, k: int) -> int:
MOD =10**9+7from collections import Counter
freq = Counter(s)
f = list(freq.values())
if len(f) < k:
return0 f.sort(reverse=True)
kth = f[k-1]
cnt = sum(1for x in f if x == kth)
total = sum(1for x in f[:k] if x == kth)
ans =1for i in range(k):
ans = ans * f[i] % MOD
from math import comb
ans = ans * comb(cnt, total) % MOD
return ans
impl Solution {
pubfncount_k_subsequences_with_max_beauty(s: String, k: i32) -> i32 {
constMOD: i64=1_000_000_007;
use std::collections::HashMap;
letmut freq = HashMap::new();
for c in s.chars() {
*freq.entry(c).or_insert(0) +=1;
}
letmut f: Vec<i64>= freq.values().cloned().collect();
if f.len() < k asusize {
return0;
}
f.sort_unstable_by(|a, b| b.cmp(a));
let kth = f[(k-1) asusize];
let cnt = f.iter().filter(|&&x| x == kth).count() asi64;
let total = f.iter().take(k asusize).filter(|&&x| x == kth).count() asi64;
letmut ans =1i64;
for i in0..k asusize {
ans = ans * f[i] %MOD;
}
ans = ans * comb(cnt, total, MOD) %MOD;
ans asi32 }
}
fncomb(n: i64, k: i64, m: i64) -> i64 {
letmut num =1i64;
letmut den =1i64;
for i in0..k {
num = num * (n - i) % m;
den = den * (i +1) % m;
}
num * powmod(den, m-2, m) % m
}
fnpowmod(mut a: i64, mut b: i64, m: i64) -> i64 {
letmut r =1i64;
while b >0 {
if b &1==1 {
r = r * a % m;
}
a = a * a % m;
b >>=1;
}
r
}
⏰ Time complexity: O(n + u log u) where n is the length of s and u is the number of unique characters. Counting frequencies is O(n), sorting is O(u log u), and the rest is O(u).
🧺 Space complexity: O(u) for storing character frequencies and sorted list.