VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-occurring-lexicographically-smallest-character-in-a-string/

⇱ Maximum occurring lexicographically smallest character in a String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum occurring lexicographically smallest character in a String

Last Updated : 11 Jul, 2025

Given a string containing lowercase characters. The task is to print the maximum occurring character in the input string. If 2 or more characters appear the same number of times, print the lexicographically (alphabetically) lowest (first) character.

Examples: 

Input: test sample 
Output:
Explanation: e't', 'e' and 's' appears 2 times, but 'e' is the lexicographically smallest character. 

Input: sample program
Output: a

In the previous article, if there are more than one character occurring the maximum number of time, then any of the characters is returned. In this post, the lexicographically smallest character of all the characters is returned.
Approach: Declare a freq[26] array which is used as a hash table to store the frequencies of each character in the input string. Iterate in the string and increase the count of freq[s[i]] for every character. Traverse the freq[] array from left to right and keep track of the character having the maximum frequency so far. The value at freq[i] represents the frequency of character (i + 'a').

Below is the implementation of the above approach: 


Output
Maximum occurring character = a

complexity Analysis:

  • Time Complexity: O(n), where n is the length of the given input string. 
  • Auxiliary Space: O(1).

Source: Sabre Interview Experience | Set 2

Comment