Maximum occurring lexicographically smallest character in a String
Last Updated :
28 Nov, 2022
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++
// C++ implementation to find
// the maximum occurring character in
// an input string which is lexicographically first
#include <bits/stdc++.h>
using namespace std;
// function to find the maximum occurring character in
// an input string which is lexicographically first
char getMaxOccurringChar(char str[])
{
// freq[] used as hash table
int freq[26] = { 0 };
// to store maximum frequency
int max = -1;
// to store the maximum occurring character
char result;
// length of 'str'
int len = strlen(str);
// get frequency of each character of 'str'
for (int i = 0; i < len; i++)
freq[str[i] - 'a']++;
// for each character, where character is obtained by
// (i + 'a') check whether it is the maximum character
// so far and accordingly update 'result'
for (int i = 0; i < 26; i++)
if (max < freq[i]) {
max = freq[i];
result = (char)(i + 'a');
}
// maximum occurring character
return result;
}
// Driver Code
int main()
{
char str[] = "sample program";
cout << "Maximum occurring character = "
<< getMaxOccurringChar(str);
return 0;
}
Java
// Java implementation to find
// the maximum occurring character in
// an input string which is lexicographically first
class GFG {
// function to find the maximum occurring character in
// an input string which is lexicographically first
static char getMaxOccurringChar(char str[]) {
// freq[] used as hash table
int freq[] = new int[26];
// to store maximum frequency
int max = -1;
// to store the maximum occurring character
char result = 0;
// length of 'str'
int len = str.length;
// get frequency of each character of 'str'
for (int i = 0; i < len; i++) {
if (str[i] != ' ') {
freq[str[i] - 'a']++;
}
}
// for each character, where character is obtained by
// (i + 'a') check whether it is the maximum character
// so far and accordingly update 'result'
for (int i = 0; i < 26; i++) {
if (max < freq[i]) {
max = freq[i];
result = (char) (i + 'a');
}
}
// maximum occurring character
return result;
}
// Driver Code
public static void main(String[] args) {
char str[] = "sample program".toCharArray();
System.out.println("Maximum occurring character = "
+ getMaxOccurringChar(str));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 implementation to find the
# maximum occurring character in an input
# string which is lexicographically first
# function to find the maximum occurring
# character in an input string which is
# lexicographically first
def getMaxOccurringChar(str):
# freq[] used as hash table
freq = [0 for i in range(100)]
# to store maximum frequency
max = -1
# to store the maximum occurring
# character length of 'str'
len__ = len(str)
# get frequency of each character of 'str'
for i in range(0, len__, 1):
freq[ord(str[i]) - ord('a')] += 1
# for each character, where character
# is obtained by (i + 'a') check whether
# it is the maximum character so far and
# accordingly update 'result'
for i in range(26):
if (max < freq[i]):
max = freq[i]
result = chr(ord('a') + i)
# maximum occurring character
return result
# Driver Code
if __name__ == '__main__':
str = "sample program"
print("Maximum occurring character =",
getMaxOccurringChar(str))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation to find
// the maximum occurring character in
// an input string which is lexicographically first
using System;
class GFG {
// function to find the maximum occurring character in
// an input string which is lexicographically first
static char getMaxOccurringChar(string str) {
// freq[] used as hash table
int[] freq = new int[26];
// to store maximum frequency
int max = -1;
// to store the maximum occurring character
char result = (char)0;
// length of 'str'
int len = str.Length;
// get frequency of each character of 'str'
for (int i = 0; i < len; i++) {
if (str[i] != ' ') {
freq[str[i] - 'a']++;
}
}
// for each character, where character is obtained by
// (i + 'a') check whether it is the maximum character
// so far and accordingly update 'result'
for (int i = 0; i < 26; i++) {
if (max < freq[i]) {
max = freq[i];
result = (char) (i + 'a');
}
}
// maximum occurring character
return result;
}
// Driver Code
public static void Main() {
string str = "sample program";
Console.WriteLine("Maximum occurring character = "
+ getMaxOccurringChar(str));
}
}
JavaScript
// C# implementation to find
// the maximum occurring character in
// an input string which is lexicographically first
using System;
class GFG {
// function to find the maximum occurring character in
// an input string which is lexicographically first
static char getMaxOccurringChar(string str) {
// freq[] used as hash table
int[] freq = new int[26];
// to store maximum frequency
int max = -1;
// to store the maximum occurring character
char result = (char)0;
// length of 'str'
int len = str.Length;
// get frequency of each character of 'str'
for (int i = 0; i < len; i++) {
if (str[i] != ' ') {
freq[str[i] - 'a']++;
}
}
// for each character, where character is obtained by
// (i + 'a') check whether it is the maximum character
// so far and accordingly update 'result'
for (int i = 0; i < 26; i++) {
if (max < freq[i]) {
max = freq[i];
result = (char) (i + 'a');
}
}
// maximum occurring character
return result;
}
// Driver Code
public static void Main() {
string str = "sample program";
Console.WriteLine("Maximum occurring character = "
+ getMaxOccurringChar(str));
}
}
OutputMaximum 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
Similar Reads
Lexicographically smallest string formed by removing at most one character Given a string s, the task is to find the lexicographically smallest string that can be formed by removing at most one character from the given string. Examples: Input: s = "abcda" Output: "abca"Explanation: One can remove 'd' to get "abca" which is the lexicographically smallest string possible. In
4 min read
Lexicographically smallest String by removing exactly K characters Given a string s consisting of only lowercase characters, the task is to find the lexicographically smallest string after removing exactly k characters from the string. But you have to modify the value of k, i.e., if the length of the string is a power of 2, reduce k by half, else multiply k by 2. Y
15 min read
Lexicographically smallest string after M operations Given a string S and integer M. The task is to perform exactly M operations to get lexicographical smallest string. In each operation, select one character optimally from the string and update it with immediate next character ( aaa -> aab ), so that string remain lexicographical smallest.Multiple
7 min read
Find maximum occurring character in a string Given string str. The task is to find the maximum occurring character in the string str.Examples:Input: geeksforgeeksOutput: eExplanation: 'e' occurs 4 times in the stringInput: testOutput: tExplanation: 't' occurs 2 times in the stringReturn the maximum occurring character in an input string using
8 min read
Lexicographically smallest String formed by extracting single character Given a string array S, the task is to find the lexicographically smallest string that can be formed by extracting a single character in each string from the string array S. Example: Input: S = ["xy", "fd"]Output: "dx"Explanation: The possible strings formed by extracting a single character from eac
5 min read