Related Articles

# Maximum repeating character for every index in given String

• Last Updated : 18 Jun, 2021

Given string str consisting of lowercase alphabets, the task is to find the maximum repeating character obtained for every character of the string. If for any index, more than one character has occurred a maximum number of times, then print the character which had occurred most recently.

Examples:

Input: str = “abbc”
Output:
a -> 1
b -> 1
b -> 2
b -> 2
Explanation:
str[0] = ‘a’. Therefore, print a -> 1.
str[1] = ‘b’. Now ‘a’ and ‘b’ have equal frequency. Since, ‘b’ is the most recently occurring character, print b -> 1.
str[2] = ‘b’. Since ‘b’is the most repeating character, print b -> 2.
str[3] = ‘c’. Since ‘b’is the most repeating character, print b -> 2.

Input: str = “htdddg”
Output:h -> 1
t -> 1
d -> 1
d -> 2
d -> 3
d -> 3

Approach: Follow the steps given below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement``// the above approach``#include ``using` `namespace` `std;`` ` `// Function to print the``// maximum repeating``// character at each index``// of the String``void` `findFreq(string str,``              ``int` `N)``{``  ``// Stores frequency of``  ``// each distinct character``  ``int` `freq[256];``  ` `  ``memset``(freq, 0,``         ``sizeof``(freq));` `  ``// Stores frequency of``  ``// maximum repeating``  ``// character``  ``int` `max = 0;` `  ``// Stores the character having``  ``// maximum frequency``  ``char` `charMax = ``'0'``;` `  ``// Traverse the String``  ``for` `(``int` `i = 0; i < N; i++)``  ``{``    ``// Stores current character``    ``char` `ch = str[i];` `    ``// Update the frequency of str[i]``    ``freq[ch]++;` `    ``// If frequency of current``    ``// character exceeds max``    ``if` `(freq[ch] >= max)``    ``{``      ``// Update max``      ``max = freq[ch];` `      ``// Update charMax``      ``charMax = ch;``    ``}` `    ``// Print the required output``    ``cout<< charMax << ``"->"` `<<``           ``max << endl;``  ``}``}` `// Driver Code``int` `main()``{``  ``string str = ``"abbc"``;` `  ``// Stores length of str``  ``int` `N = str.size();` `  ``findFreq(str, N);``}` `// This code is contributed by Rajput-Ji`

## Java

 `// Java program to implement``// the above approach` `import` `java.util.*;` `public` `class` `GFG {` `    ``// Function to print the maximum repeating``    ``// character at each index of the string``    ``public` `static` `void` `findFreq(String str,``                                ``int` `N)``    ``{` `        ``// Stores frequency of``        ``// each distinct character``        ``int``[] freq = ``new` `int``[``256``];` `        ``// Stores frequency of maximum``        ``// repeating character``        ``int` `max = ``0``;` `        ``// Stores the character having``        ``// maximum frequency``        ``char` `charMax = ``'0'``;` `        ``// Traverse the string``        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// Stores current character``            ``char` `ch = str.charAt(i);` `            ``// Update the frequency of str[i]``            ``freq[ch]++;` `            ``// If frequency of current``            ``// character exceeds max``            ``if` `(freq[ch] >= max) {` `                ``// Update max``                ``max = freq[ch];` `                ``// Update charMax``                ``charMax = ch;``            ``}` `            ``// Print the required output``            ``System.out.println(``                ``charMax + ``" -> "` `+ max);``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``String str = ``"abbc"``;` `        ``// Stores length of str``        ``int` `N = str.length();` `        ``findFreq(str, N);``    ``}``}`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to print the maximum repeating``# character at each index of the string``def` `findFreq(strr, N):``    ` `    ``# Stores frequency of``    ``# each distinct character``    ``freq ``=` `[``0``] ``*` `256` `    ``# Stores frequency of maximum``    ``# repeating character``    ``max` `=` `0` `    ``# Stores the character having``    ``# maximum frequency``    ``charMax ``=` `'0'` `    ``# Traverse the string``    ``for` `i ``in` `range``(N):``        ` `        ``# Stores current character``        ``ch ``=` `ord``(strr[i])` `        ``# Update the frequency of strr[i]``        ``freq[ch] ``+``=` `1` `        ``# If frequency of current``        ``# character exceeds max``        ``if` `(freq[ch] >``=` `max``):` `            ``# Update max``            ``max` `=` `freq[ch]` `            ``# Update charMax``            ``charMax ``=` `ch` `        ``# Print the required output``        ``print``(``chr``(charMax), ``"->"``, ``max``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``strr ``=` `"abbc"` `    ``# Stores length of strr``    ``N ``=` `len``(strr)` `    ``findFreq(strr, N)` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program to implement``// the above approach``using` `System;``class` `GFG{` `// Function to print the maximum repeating``// character at each index of the string``public` `static` `void` `findFreq(String str,``                            ``int` `N)``{``  ``// Stores frequency of``  ``// each distinct character``  ``int``[] freq = ``new` `int``[256];` `  ``// Stores frequency of maximum``  ``// repeating character``  ``int` `max = 0;` `  ``// Stores the character having``  ``// maximum frequency``  ``char` `charMax = ``'0'``;` `  ``// Traverse the string``  ``for` `(``int` `i = 0; i < N; i++)``  ``{``    ``// Stores current character``    ``char` `ch = str[i];` `    ``// Update the frequency of``    ``// str[i]``    ``freq[ch]++;` `    ``// If frequency of current``    ``// character exceeds max``    ``if` `(freq[ch] >= max)``    ``{``      ``// Update max``      ``max = freq[ch];` `      ``// Update charMax``      ``charMax = ch;``    ``}` `    ``// Print the required output``    ``Console.WriteLine(charMax +``                      ``" -> "` `+ max);``  ``}``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``  ``String str = ``"abbc"``;` `  ``// Stores length of str``  ``int` `N = str.Length;` `  ``findFreq(str, N);``}``}` `// This code is contributed by shikhasingrajput`

## Javascript

 ``
Output:
```a -> 1
b -> 1
b -> 2
b -> 2```

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up