Most Frequent Character
EasyPractice library · +10 XPhash mapstringscounting
Given a string S, find the character that appears most frequently. If there is a tie for the most frequent character, output the character that comes first lexicographically (alphabetically).
Input
A single line containing the string S.
Output
A single character: the most frequent character in S, with ties broken lexicographically.
Constraints
The string S consists of lowercase English letters ('a'-'z'). The length of S is between 1 and 1000, inclusive.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
programming
Output
g
'r' and 'g' appear twice. 'g' comes before 'r' alphabetically.
Example 2
Input
aabbbcccc
Output
c
'c' appears 4 times, which is the maximum frequency.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
programming
Example 2 input
aabbbcccc