Maximum repeating character for every index in given String Last Updated : 18 Jun, 2021 Comments Improve Suggest changes Like Article Like Report 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 -> 1b -> 1b -> 2b -> 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 -> 1t -> 1d -> 1d -> 2d -> 3d -> 3 Approach: Follow the steps given below to solve the problem: Initialize an array, say freq[] to store the frequency of each distinct character in the string.Initialize two variables, say max, charMax to store the frequency of maximum repeating character and the maximum repeating character respectively.Traverse the string and update the frequency of current character, update the value of max and charMax.Finally, print the value of charMax and max after every character traversal. Below is the implementation of the above approach: C++ // C++ Program to implement // the above approach #include <bits/stdc++.h> 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 <script> // JavaScript Program to implement // the above approach // Function to print the // maximum repeating // character at each index // of the String function findFreq(str, N) { // Stores frequency of // each distinct character let freq = new Array(256).fill(0); // Stores frequency of // maximum repeating // character let max = 0; // Stores the character having // maximum frequency let charMax = '0'; // Traverse the String for (let i = 0; i < N; i++) { // Stores current character let ch = str[i].charCodeAt(0); // 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 document.write(String.fromCharCode(charMax) + "->" + max + "<br>"); } } // Driver Code let str = "abbc"; // Stores length of str let N = str.length; findFreq(str, N); // This code is contributed by gfgking </script> Output: a -> 1 b -> 1 b -> 2 b -> 2 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Maximum repeating character for every index in given String K kunalsg18elec Follow Improve Article Tags : Strings Hash DSA frequency-counting strings +1 More Practice Tags : HashStringsStrings Similar Reads Maximum consecutive repeating character in string Given a string s, the task is to find the maximum consecutive repeating character in the string.Note: We do not need to consider the overall count, but the count of repeating that appears in one place.Examples: Input: s = "geeekk"Output: eExplanation: character e comes 3 times consecutively which is 11 min read Maximum repeated frequency of characters in a given string Given a string S, the task is to find the count of maximum repeated frequency of characters in the given string S.Examples: Input: S = "geeksgeeks" Output: Frequency 2 is repeated 3 times Explanation: Frequency of characters in the given string - {"g": 2, "e": 4, "k": 2, "s": 2} The frequency 2 is r 6 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 Largest index for each distinct character in given string with frequency K Given a string S consisting of lowercase English letters and an integer K, the task is to find, for each distinct character in S, the largest index having this character exactly K times. If no such characters exist, print -1. Print the result in a lexicographical ordering.Note: Consider 0-based inde 8 min read Find last index of a character in a string Given a string str and a character x, find last index of x in str. Examples : Input : str = "geeks", x = 'e' Output : 2 Last index of 'e' in "geeks" is: 2 Input : str = "Hello world!", x = 'o' Output : 7 Last index of 'o' is: 7 Recommended PracticeLast index of a character in the stringTry It! Metho 8 min read Split string to get maximum common characters Given a string S of length, N. Split them into two strings such that the number of common characters between the two strings is maximized and return the maximum number of common characters. Examples: Input: N = 6, S = abccbaOutput: 3Explanation: Splitting two strings as "abc" and "cba" has at most 3 6 min read Lexicographically largest string formed in minimum moves by replacing characters of given String Given a string S consisting of N lowercase English characters, the task is to print the lexicographically, the largest string obtained using only the minimum number of moves needed to modify the string S to a string containing the first min(N, 26) lower case English alphabet, by replacing any charac 10 min read Substring of length K having maximum frequency in the given string Given a string str, the task is to find the substring of length K which occurs the maximum number of times. If more than one string occurs maximum number of times, then print the lexicographically smallest substring. Examples: Input: str = "bbbbbaaaaabbabababa", K = 5Output: ababaExplanation:The sub 14 min read Print Longest substring without repeating characters Given a string s having lowercase characters, find the length of the longest substring without repeating characters. Examples:Input: s = âgeeksforgeeksâOutput: 7 Explanation: The longest substrings without repeating characters are âeksforgâ and âksforgeâ, with lengths of 7.Input: s = âaaaâOutput: 1E 14 min read Maximize ascii sum removing K characters in String Given a string s and an integer K, the task is to remove K characters from the string such that the sum of ASCII (American Standard Code for Information Interchange) values of the remaining characters is maximized. Examples: Input: s = "word", K = 2Output: 233Explanation: We need to remove exactly 2 9 min read Like