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: e
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:
C++
#include <bits/stdc++.h>
using namespace std;
char getMaxOccurringChar( char str[])
{
int freq[26] = { 0 };
int max = -1;
char result;
int len = strlen (str);
for ( int i = 0; i < len; i++)
freq[str[i] - 'a' ]++;
for ( int i = 0; i < 26; i++)
if (max < freq[i]) {
max = freq[i];
result = ( char )(i + 'a' );
}
return result;
}
int main()
{
char str[] = "sample program" ;
cout << "Maximum occurring character = "
<< getMaxOccurringChar(str);
return 0;
}
|
Java
class GFG {
static char getMaxOccurringChar( char str[]) {
int freq[] = new int [ 26 ];
int max = - 1 ;
char result = 0 ;
int len = str.length;
for ( int i = 0 ; i < len; i++) {
if (str[i] != ' ' ) {
freq[str[i] - 'a' ]++;
}
}
for ( int i = 0 ; i < 26 ; i++) {
if (max < freq[i]) {
max = freq[i];
result = ( char ) (i + 'a' );
}
}
return result;
}
public static void main(String[] args) {
char str[] = "sample program" .toCharArray();
System.out.println( "Maximum occurring character = "
+ getMaxOccurringChar(str));
}
}
|
Python3
def getMaxOccurringChar( str ):
freq = [ 0 for i in range ( 100 )]
max = - 1
len__ = len ( str )
for i in range ( 0 , len__, 1 ):
freq[ ord ( str [i]) - ord ( 'a' )] + = 1
for i in range ( 26 ):
if ( max < freq[i]):
max = freq[i]
result = chr ( ord ( 'a' ) + i)
return result
if __name__ = = '__main__' :
str = "sample program"
print ( "Maximum occurring character =" ,
getMaxOccurringChar( str ))
|
C#
using System;
class GFG {
static char getMaxOccurringChar( string str) {
int [] freq = new int [26];
int max = -1;
char result = ( char )0;
int len = str.Length;
for ( int i = 0; i < len; i++) {
if (str[i] != ' ' ) {
freq[str[i] - 'a' ]++;
}
}
for ( int i = 0; i < 26; i++) {
if (max < freq[i]) {
max = freq[i];
result = ( char ) (i + 'a' );
}
}
return result;
}
public static void Main() {
string str = "sample program" ;
Console.WriteLine( "Maximum occurring character = "
+ getMaxOccurringChar(str));
}
}
|
Javascript
using System;
class GFG {
static char getMaxOccurringChar(string str) {
int[] freq = new int[26];
int max = -1;
char result = (char)0;
int len = str.Length;
for (int i = 0; i < len; i++) {
if (str[i] != ' ' ) {
freq[str[i] - 'a' ]++;
}
}
for (int i = 0; i < 26; i++) {
if (max < freq[i]) {
max = freq[i];
result = (char) (i + 'a' );
}
}
return result;
}
public static void Main() {
string str = "sample program" ;
Console.WriteLine( "Maximum occurring character = "
+ getMaxOccurringChar(str));
}
}
|
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
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Nov, 2022
Like Article
Save Article