2842. Count K-Subsequences of a String With Maximum Beauty
Problem Statement
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
s
are:"
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 109 + 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 characterc
occurs 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:
Example 2:
Constraints:
1 <= s.length <= 2 * 105
1 <= k <= s.length
s
consists only of lowercase English letters.
Intuition
Links
https://leetcode.com/problems/count-k-subsequences-of-a-string-with-maximum-beauty/
Video Links
https://www.youtube.com/watch?v=74c6JV9DlHg&t=2s&ab_channel=AryanMittal
Approach 1:
Approach 2:
Approach 3:
Approach 4:
Similar Problems
Last updated