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++
#include <bits/stdc++.h>
using namespace std;
void findFreq(string str,
int N)
{
int freq[256];
memset (freq, 0,
sizeof (freq));
int max = 0;
char charMax = '0' ;
for ( int i = 0; i < N; i++)
{
char ch = str[i];
freq[ch]++;
if (freq[ch] >= max)
{
max = freq[ch];
charMax = ch;
}
cout<< charMax << "->" <<
max << endl;
}
}
int main()
{
string str = "abbc" ;
int N = str.size();
findFreq(str, N);
}
|
Java
import java.util.*;
public class GFG {
public static void findFreq(String str,
int N)
{
int [] freq = new int [ 256 ];
int max = 0 ;
char charMax = '0' ;
for ( int i = 0 ; i < N; i++) {
char ch = str.charAt(i);
freq[ch]++;
if (freq[ch] >= max) {
max = freq[ch];
charMax = ch;
}
System.out.println(
charMax + " -> " + max);
}
}
public static void main(String[] args)
{
String str = "abbc" ;
int N = str.length();
findFreq(str, N);
}
}
|
Python3
def findFreq(strr, N):
freq = [ 0 ] * 256
max = 0
charMax = '0'
for i in range (N):
ch = ord (strr[i])
freq[ch] + = 1
if (freq[ch] > = max ):
max = freq[ch]
charMax = ch
print ( chr (charMax), "->" , max )
if __name__ = = '__main__' :
strr = "abbc"
N = len (strr)
findFreq(strr, N)
|
C#
using System;
class GFG{
public static void findFreq(String str,
int N)
{
int [] freq = new int [256];
int max = 0;
char charMax = '0' ;
for ( int i = 0; i < N; i++)
{
char ch = str[i];
freq[ch]++;
if (freq[ch] >= max)
{
max = freq[ch];
charMax = ch;
}
Console.WriteLine(charMax +
" -> " + max);
}
}
public static void Main(String[] args)
{
String str = "abbc" ;
int N = str.Length;
findFreq(str, N);
}
}
|
Javascript
<script>
function findFreq(str, N) {
let freq = new Array(256).fill(0);
let max = 0;
let charMax = '0' ;
for (let i = 0; i < N; i++) {
let ch = str[i].charCodeAt(0);
freq[ch]++;
if (freq[ch] >= max) {
max = freq[ch];
charMax = ch;
}
document.write(String.fromCharCode(charMax) + "->" +
max + "<br>" );
}
}
let str = "abbc" ;
let N = str.length;
findFreq(str, N);
</script>
|
Output:
a -> 1
b -> 1
b -> 2
b -> 2
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...