Write an efficient function to return maximum occurring character in the input string e.g., if input string is “test” then function should return ‘t’.
One obvious approach to solve this problem would be to sort the input string and then traverse through the sorted string to find the character which is occurring maximum number of times. Let us see if we can improve on this. So we will use a technique called ‘Hashing’. In this, when we traverse through the string, we would hash each character into an array of ASCII characters.
Input string = “test” 1: Construct character count array from the input string. count['e'] = 1 count['s'] = 1 count['t'] = 2 2: Return the index of maximum value in count array (returns ‘t’).
Typically, ASCII characters are 256, so we use our Hash array size as 256. But if we know that our input string will have characters with value from 0 to 127 only, we can limit Hash array size as 128. Similarly, based on extra info known about input string, the Hash array size can be limited to 26.
Max occurring character is s
Time Complexity: O(n)
Space Complexity: O(1) — Because we are using fixed space (Hash array) irrespective of input string size.
If more than one characters have the same count and that count is maximum then the function returns the first character with maximum count in input string. For example if input string is “test sample” then there are three characters with same and maximum count two i.e. “t”, “e” and “s” but our program will result “t” because “t” comes first in input string. Similarly, the output for “cbbbbccc” would be “c”.
As an variation to the above program, think about the change in code if you want to output “e” for input “test sample” i.e. the character of maximum count and that character should be of least ASCII value. For “cbbbbccc”, output should be “b”. Try it out !
Also, can you think of improvement if we can avoid two loopings in the above? Basically, you need to figure out if we can solve the same problem with one loop itself instead of two loops.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Maximum occurring character in an input string | Set-2
- Check if max occurring character of one string appears same no. of times in other
- 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 consecutive repeating character in string
- Maximum number of characters between any two same character in a string
- 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
- Find a string such that every character is lexicographically greater than its immediate next character
- Count the number of vowels occurring in all the substrings of given string
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Map every character of one string to another such that all occurrences are mapped to the same character