Given a string, the task is to find the maximum consecutive repeating character in a string.
Note: We do not need to consider the overall count, but the count of repeating that appears in one place.
Examples:
Input : str = "geeekk"
Output : e
Input : str = "aaaabbcbbb"
Output : a
The simple solution to this problem is to use two for loops. The outer loop considers the current character, the inner loop counts occurrences of the current character. If the count goes beyond the current maximum count, we update the result.
C++
#include<bits/stdc++.h>
using namespace std;
char maxRepeating(string str)
{
int len = str.length();
int count = 0;
char res = str[0];
for ( int i=0; i<len; i++)
{
int cur_count = 1;
for ( int j=i+1; j<len; j++)
{
if (str[i] != str[j])
break ;
cur_count++;
}
if (cur_count > count)
{
count = cur_count;
res = str[i];
}
}
return res;
}
int main()
{
string str = "aaaabbaaccde" ;
cout << maxRepeating(str);
return 0;
}
|
Java
public class GFG {
static char maxRepeating(String str)
{
int len = str.length();
int count = 0 ;
char res = str.charAt( 0 );
for ( int i= 0 ; i<len; i++)
{
int cur_count = 1 ;
for ( int j=i+ 1 ; j<len; j++)
{
if (str.charAt(i) != str.charAt(j))
break ;
cur_count++;
}
if (cur_count > count)
{
count = cur_count;
res = str.charAt(i);
}
}
return res;
}
public static void main(String args[])
{
String str = "aaaabbaaccde" ;
System.out.println(maxRepeating(str));
}
}
|
Python 3
def maxRepeating( str ):
l = len ( str )
count = 0
res = str [ 0 ]
for i in range (l):
cur_count = 1
for j in range (i + 1 , l):
if ( str [i] ! = str [j]):
break
cur_count + = 1
if cur_count > count :
count = cur_count
res = str [i]
return res
if __name__ = = "__main__" :
str = "aaaabbaaccde"
print (maxRepeating( str ))
|
C#
using System;
class GFG
{
static char maxRepeating( string str)
{
int len = str.Length;
int count = 0;
char res = str[0];
for ( int i = 0; i < len; i++)
{
int cur_count = 1;
for ( int j = i + 1; j < len; j++)
{
if (str[i] != str[j])
break ;
cur_count++;
}
if (cur_count > count)
{
count = cur_count;
res = str[i];
}
}
return res;
}
public static void Main()
{
string str = "aaaabbaaccde" ;
Console.Write(maxRepeating(str));
}
}
|
Javascript
<script>
function maxRepeating(str)
{
let len = str.length;
let count = 0;
let res = str[0];
for (let i=0; i<len; i++)
{
let cur_count = 1;
for (let j=i+1; j<len; j++)
{
if (str[i] != str[j])
break ;
cur_count++;
}
if (cur_count > count)
{
count = cur_count;
res = str[i];
}
}
return res;
}
let str = "aaaabbaaccde" ;
document.write(maxRepeating(str));
</script>
|
PHP
<?php
function maxRepeating( $str )
{
$len = strlen ( $str );
$count = 0;
$res = $str [0];
for ( $i = 0; $i < $len ; $i ++)
{
$cur_count = 1;
for ( $j = $i +1; $j < $len ; $j ++)
{
if ( $str [ $i ] != $str [ $j ])
break ;
$cur_count ++;
}
if ( $cur_count > $count )
{
$count = $cur_count ;
$res = $str [ $i ];
}
}
return $res ;
}
$str = "aaaabbaaccde" ;
echo maxRepeating( $str );
?>
|
Output:
a
Time Complexity : O(n^2)
Space Complexity : O(1)
An efficient solution is to run only one loop. The idea is to reset the count as 1 as soon as we find a character not matching with the previous.
C++
#include<bits/stdc++.h>
using namespace std;
char maxRepeating(string str)
{
int n = str.length();
int count = 0;
char res = str[0];
int cur_count = 1;
for ( int i=0; i<n; i++)
{
if (i < n-1 && str[i] == str[i+1])
cur_count++;
else
{
if (cur_count > count)
{
count = cur_count;
res = str[i];
}
cur_count = 1;
}
}
return res;
}
int main()
{
string str = "aaaabbaaccde" ;
cout << maxRepeating(str);
return 0;
}
|
Java
class GFG {
static char maxRepeating(String str)
{
int n = str.length();
int count = 0 ;
char res = str.charAt( 0 );
int cur_count = 1 ;
for ( int i = 0 ; i < n; i++)
{
if (i < n - 1 && str.charAt(i) == str.charAt(i + 1 ))
cur_count++;
else
{
if (cur_count > count)
{
count = cur_count;
res = str.charAt(i);
}
cur_count = 1 ;
}
}
return res;
}
public static void main(String args[])
{
String str = "aaaabbaaccde" ;
System.out.println(maxRepeating(str));
}
}
|
Python 3
def maxRepeating( str ):
n = len ( str )
count = 0
res = str [ 0 ]
cur_count = 1
for i in range (n):
if (i < n - 1 and
str [i] = = str [i + 1 ]):
cur_count + = 1
else :
if cur_count > count:
count = cur_count
res = str [i]
cur_count = 1
return res
if __name__ = = "__main__" :
str = "aaaabbaaccde"
print (maxRepeating( str ))
|
C#
using System;
class GFG
{
static char maxRepeating( string str)
{
int n = str.Length;
int count = 0;
char res = str[0];
int cur_count = 1;
for ( int i = 0; i < n; i++)
{
if (i < n - 1 &&
str[i] == str[i + 1])
cur_count++;
else
{
if (cur_count > count)
{
count = cur_count;
res = str[i];
}
cur_count = 1;
}
}
return res;
}
public static void Main()
{
string str = "aaaabbaaccde" ;
Console.Write(maxRepeating(str));
}
}
|
Javascript
<script>
function maxRepeating( str)
{
var n = str.length;
var count = 0;
var res = str[0];
var cur_count = 1;
for ( var i=0; i<n; i++)
{
if (i < n-1 && str[i] == str[i+1])
cur_count++;
else
{
if (cur_count > count)
{
count = cur_count;
res = str[i];
}
cur_count = 1;
}
}
return res;
}
var str = "aaaabbaaccde" ;
document.write( maxRepeating(str));
</script>
|
PHP
<?php
function maxRepeating( $str )
{
$n = strlen ( $str );
$count = 0;
$res = $str [0];
$cur_count = 1;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $i < $n - 1 &&
$str [ $i ] == $str [ $i + 1])
$cur_count ++;
else
{
if ( $cur_count > $count )
{
$count = $cur_count ;
$res = $str [ $i ];
}
$cur_count = 1;
}
}
return $res ;
}
$str = "aaaabbaaccde" ;
echo maxRepeating( $str );
?>
|
Output:
a
Time Complexity : O(n)
Space Complexity : O(1)
Another Approach:
- Start by including the required header files and using the standard namespace.
- Define a function maxRepeating that takes a string as input and returns a character as output.
- Get the length of the input string and initialize two variables maxCount and curCount to 0 and 1 respectively. Also, initialize the variable res with the first character of the string.
- Traverse the string starting from the second character (index 1) to the end using a for loop.
- Check if the current character is the same as the previous character (i.e., str[i] == str[i-1]).
- If the current character is the same as the previous character, increment the curCount variable.
- If the current character is not the same as the previous character, compare the curCount variable with the maxCount variable to see if it is greater.
- If the curCount variable is greater than the maxCount variable, update the maxCount variable with the value of curCount and set the res variable to the previous character (i.e., str[i-1]).
- Reset the curCount variable to 1.
- After the loop, check if the curCount variable is greater than the maxCount variable for the last character.
- If the curCount variable is greater than the maxCount variable, update the maxCount variable with the value of curCount and set the res variable to the last character (i.e., str[n-1]).
- Return the res variable.
- In the main function, define a string variable str with the input string and call the maxRepeating function with str as argument.
- Print the output of the maxRepeating function.
- End of program.
C++
#include <iostream>
using namespace std;
char maxRepeating(string str) {
int n = str.length();
int maxCount = 0, curCount = 1;
char res = str[0];
for ( int i = 1; i < n; i++) {
if (str[i] == str[i - 1]) {
curCount++;
} else {
if (curCount > maxCount) {
maxCount = curCount;
res = str[i - 1];
}
curCount = 1;
}
}
if (curCount > maxCount) {
maxCount = curCount;
res = str[n - 1];
}
return res;
}
int main() {
string str = "aaaabbcbbb" ;
cout << maxRepeating(str);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static char maxRepeating(String str) {
int n = str.length();
int maxCount = 0 , curCount = 1 ;
char res = str.charAt( 0 );
for ( int i = 1 ; i < n; i++) {
if (str.charAt(i) == str.charAt(i - 1 )) {
curCount++;
} else {
if (curCount > maxCount) {
maxCount = curCount;
res = str.charAt(i - 1 );
}
curCount = 1 ;
}
}
if (curCount > maxCount) {
maxCount = curCount;
res = str.charAt(n - 1 );
}
return res;
}
public static void main(String[] args) {
String str = "aaaabbcbbb" ;
System.out.println(maxRepeating(str));
}
}
|
Python3
def max_repeating(string):
n = len (string)
max_count = 0
cur_count = 1
res = string[ 0 ]
for i in range ( 1 , n):
if string[i] = = string[i - 1 ]:
cur_count + = 1
else :
if cur_count > max_count:
max_count = cur_count
res = string[i - 1 ]
cur_count = 1
if cur_count > max_count:
max_count = cur_count
res = string[n - 1 ]
return res
string = "aaaabbcbbb"
print (max_repeating(string))
|
C#
using System;
class Program
{
static char MaxRepeating( string str)
{
int n = str.Length;
int maxCount = 0, curCount = 1;
char res = str[0];
for ( int i = 1; i < n; i++)
{
if (str[i] == str[i - 1])
{
curCount++;
}
else
{
if (curCount > maxCount)
{
maxCount = curCount;
res = str[i - 1];
}
curCount = 1;
}
}
if (curCount > maxCount)
{
maxCount = curCount;
res = str[n - 1];
}
return res;
}
static void Main()
{
string str = "aaaabbcbbb" ;
Console.WriteLine(MaxRepeating(str));
}
}
|
Javascript
function maxRepeating(str) {
const n = str.length;
let maxCount = 0, curCount = 1;
let res = str[0];
for (let i = 1; i < n; i++) {
if (str[i] === str[i - 1]) {
curCount++;
} else {
if (curCount > maxCount) {
maxCount = curCount;
res = str[i - 1];
}
curCount = 1;
}
}
if (curCount > maxCount) {
maxCount = curCount;
res = str[n - 1];
}
return res;
}
const str = "aaaabbcbbb" ;
console.log(maxRepeating(str));
|
Time complexity: O(n)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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 :
17 Oct, 2023
Like Article
Save Article