3 - Most Frequent Character
Time limit: 1000 ms
Memory limit: 250 MB

You are given a lowercase English string S. Your task is to find the character that appears the most number of times in the string.

If more than one character has the same highest frequency, print the lexicographically smallest one (the one that comes first in alphabetical order).

You need to perform this task for multiple test cases.

Input
  • The first line contains an integer T, the number of test cases.

  • Each of the next T lines contains a single string S.

Output

For each test case, print a single lowercase letter that appears most frequently in the given string.

If there are multiple answers, print the smallest character in lexicographical order.

Constraints
  • 1 ≤ T ≤ 100

  • 1 ≤ |S| ≤ 100000

  • String S contains only lowercase English letters (a to z).

Example Input Example Output
3
aabbbc
xyz
bbaacc
b
x
a
Explanation

Test Case 1:
String = aabbbc
Frequencies → a:2, b:3, c:1
The most frequent character is b.
Output → b

Test Case 2:
String = xyz
All characters appear once.
Lexicographically smallest is x.
Output → x

Test Case 3:
String = bbaacc
Frequencies → a:2, b:2, c:2
All have equal frequency; smallest is a.
Output → a


Sign in to Submit