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.
Input: test sample
‘t’, ‘e’ and ‘s’ appears 2 times, but ‘e’ is the lexicographically smallest character.
Input: sample program
In the previous article, if there are more than one characters 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 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.T raverse 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:
Maximum occurring character = a
Time Complexity: O(n).
Auxiliary Space: O(1).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Return maximum occurring character in an input string
- Check if max occurring character of one string appears same no. of times in other
- Frequency of maximum occurring subsequence in given string
- Generate two output strings depending upon occurrence of character in input string.
- Generate two output strings depending upon occurrence of character in input string in Python
- Count substrings with each character occurring at most k times
- Print the most occurring character in an array of strings
- NFA to accept strings that has atleast one character occurring in a multiple of 3
- Maximum number of characters between any two same character in a string
- Maximum consecutive repeating character in string
- Find a string such that every character is lexicographically greater than its immediate next character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace every character of string by character whose ASCII value is K times more than it
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Count of Distinct Substrings occurring consecutively in a given String
- Count the number of vowels occurring in all the substrings of given string
- Replace all occurrences of character X with character Y in given string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Replace every character of a string by a different character
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.