LeetCode in Kotlin

2842. Count K-Subsequences of a String With Maximum Beauty

Hard

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:

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

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:

Solution

@Suppress("NAME_SHADOWING")
class Solution {
    fun countKSubsequencesWithMaxBeauty(s: String, k: Int): Int {
        var k = k
        val n = s.length
        val count = IntArray(26)
        for (i in 0 until n) {
            count[s[i].code - 'a'.code]++
        }
        count.sort()
        if (k > 26 || count[26 - k] == 0) {
            return 0
        }
        var res: Long = 1
        var comb: Long = 1
        val mod = 1e9.toLong() + 7
        val bar = count[26 - k].toLong()
        var pend: Long = 0
        for (freq in count) {
            if (freq > bar) {
                k--
                res = res * freq % mod
            }
            if (freq.toLong() == bar) {
                pend++
            }
        }
        for (i in 0 until k) {
            comb = comb * (pend - i) / (i + 1)
            res = res * bar % mod
        }
        return (res * comb % mod).toInt()
    }
}