Given a string s, return all characters that appear the most frequently in s.
If multiple characters have the same highest frequency, return all of them. Return the result as a sorted list of characters.
Function Signature
def most_frequent_chars(s: str) -> list[str]:
Rules
-
Each character in the result is a one-character string.
-
Characters are compared exactly as they appear: an uppercase letter and its lowercase form are different characters, and a space counts like any other character.
-
Sort the result in ascending order of character code (ASCII order). A space comes before digits, digits come before uppercase letters, and uppercase letters come before lowercase letters.
Constraints
-
1 <= len(s) <= 10^5
-
s
consists of lowercase and uppercase English letters, digits, and spaces.
Examples
Input: s = "aabbcc"
Output: ["a", "b", "c"]
Characters 'a', 'b' and 'c' each appear 2 times, which is the highest frequency, so all three are returned.
Input: s = "banana"
Output: ["a"]
'a' appears 3 times, 'n' appears 2 times and 'b' appears once, so 'a' alone has the highest frequency.
Input: s = "Zz9 z9Z"
Output: ["9", "Z", "z"]
'Z', 'z' and '9' each appear twice and the space appears once. In ASCII order '9' (code 57) comes before 'Z' (code 90), which comes before 'z' (code 122).