#K3041. K-th Most Frequent Character
K-th Most Frequent Character
K-th Most Frequent Character
You are given a string s and an integer k. Your task is to find the k-th most frequent character in the string. In other words, you should rank the characters by their frequency in s in descending order. If two characters have the same frequency, the one with the lexicographically smaller value (i.e. the one that comes first in alphabetical order) is considered to have a higher ranking.
If k is greater than the number of distinct characters in the string, output None
.
Note: In the frequency ranking, ties are broken by comparing characters based on the ordering of their ASCII values, i.e., using the lexicographical order.
The mathematical criteria behind the ranking can be summarized in LaTeX as follows:
$$\text{Sort the set } \{(c, f(c))\}_{c \in s} \text{ in descending order by } f(c) \text{ and ascending order by } c.$$
inputFormat
The input consists of two lines:
- The first line contains the string s, which is non-empty.
- The second line contains an integer k.
outputFormat
Output a single line containing the k-th most frequent character. If such a character does not exist (i.e. if k is greater than the number of distinct characters), output None
.
BANANA
1
A