Find all characters tied for the highest frequency in a string

Quick Overview

Given a string of letters, digits and spaces, return every character that appears with the highest frequency, sorted by character code. It tests counting with a hash map, keeping all ties instead of one winner, and producing a deterministic output order.

Find all characters tied for the highest frequency in a string

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text 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. ```text Input: s = "banana" Output: ["a"] ``` 'a' appears 3 times, 'n' appears 2 times and 'b' appears once, so 'a' alone has the highest frequency. ```text 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).

Overview: Given a string of letters, digits and spaces, return every character that appears with the highest frequency, sorted by character code. It tests counting with a hash map, keeping all ties instead of one winner, and producing a deterministic output order.

PracHub logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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).