Maximum consecutive repeating character in string - GeeksforGeeks

10 May, 2021
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++
Java
Python 3
C#
PHP
Javascript
// C++ program to find the maximum consecutive// repeating character in given string#include<bits/stdc++.h>using namespace std; // function to find out the maximum repeating// character in given stringchar maxRepeating(string str){    int len = str.length();    int count = 0;     // Find the maximum repeating character    // starting from str[i]    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++;        }         // Update result if required        if (cur_count > count)        {            count = cur_count;            res = str[i];        }    }    return res;} // Driver codeint main(){     string str = "aaaabbaaccde";    cout << maxRepeating(str);    return 0;}
// Java program to find the maximum consecutive// repeating character in given stringpublic class GFG {         // function to find out the maximum repeating    // character in given string    static char maxRepeating(String str)    {        int len = str.length();        int count = 0;         // Find the maximum repeating character        // starting from str[i]        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++;            }             // Update result if required            if (cur_count > count)            {                count = cur_count;                res = str.charAt(i);            }        }        return res;    }     // Driver code    public static void main(String args[])    {         String str = "aaaabbaaccde";        System.out.println(maxRepeating(str));    }}// This code is contributed by Sumit Ghosh
# Python 3 program to find the# maximum consecutive repeating# character in given string # function to find out the maximum# repeating character in given stringdef maxRepeating(str):     l = len(str)    count = 0     # Find the maximum repeating    # character starting from str[i]    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         # Update result if required        if cur_count > count :            count = cur_count            res = str[i]    return res # Driver codeif __name__ == "__main__":     str = "aaaabbaaccde"    print(maxRepeating(str)) # This code is contributed# by ChitraNayal
// C# program to find the maximum// consecutive repeating character// in given stringusing System; class GFG{ // function to find out the maximum// repeating character in given stringstatic char maxRepeating(string str){    int len = str.Length;    int count = 0;    char res = str[0];         // Find the maximum repeating    // character starting from str[i]    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++;        }         // Update result if required        if (cur_count > count)        {            count = cur_count;            res = str[i];        }    }    return res;} // Driver codepublic static void Main(){    string str = "aaaabbaaccde";    Console.Write(maxRepeating(str));}} // This code is contributed// by ChitraNayal
<?php//PHP program to find the maximum consecutive// repeating character in given string // function to find out the maximum repeating// character in given stringfunction  maxRepeating( $str){     $len = strlen($str);    $count = 0;          // Find the maximum repeating character    // starting from str[i]     $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++;        }         // Update result if required        if ($cur_count > $count)        {            $count = $cur_count;            $res = $str[$i];        }    }    return $res;} // Driver code    $str = "aaaabbaaccde";    echo  maxRepeating($str); // This code is contributed by ajit?>
<script> // Javascript program to find the maximum consecutive// repeating character in given string         // function to find out the maximum repeating    // character in given string    function maxRepeating(str)    {        let len = str.length;        let count = 0;           // Find the maximum repeating character        // starting from str[i]        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++;            }               // Update result if required            if (cur_count > count)            {                count = cur_count;                res = str[i];            }        }        return res;    }         // Driver code    let str = "aaaabbaaccde";    document.write(maxRepeating(str));         // This code is contributed by rag2127 </script>
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++
Java
Python 3
C#
PHP
Javascript
// C++ program to find the maximum consecutive// repeating character in given string#include<bits/stdc++.h>using namespace std; // Returns the maximum repeating character in a// given stringchar maxRepeating(string str){    int n = str.length();    int count = 0;    char res = str[0];    int cur_count = 1;     // Traverse string except last character    for (int i=0; i<n; i++)    {        // If current character matches with next        if (i < n-1 && str[i] == str[i+1])            cur_count++;         // If doesn't match, update result        // (if required) and reset count        else        {            if (cur_count > count)            {                count = cur_count;                res = str[i];            }            cur_count = 1;        }    }     return res;} // Driver codeint main(){    string str = "aaaabbaaccde";    cout << maxRepeating(str);    return 0;}
// Java program to find the maximum consecutive// repeating character in given stringclass GFG {         // function to find out the maximum repeating    // character in given string    static char maxRepeating(String str)    {        int n = str.length();        int count = 0;        char res = str.charAt(0);        int cur_count = 1;         // Traverse string except last character        for (int i = 0; i < n; i++)        {            // If current character matches with next            if (i < n - 1 && str.charAt(i) == str.charAt(i + 1))                cur_count++;             // If doesn't match, update result            // (if required) and reset count            else            {                if (cur_count > count)                {                    count = cur_count;                    res = str.charAt(i);                }                cur_count = 1;            }        }        return res;    }     // Driver code    public static void main(String args[])    {        String str = "aaaabbaaccde";        System.out.println(maxRepeating(str));    }} // This code is contributed by Sudeep Mukherjee
# Python 3 program to find the# maximum consecutive repeating# character in given string # Returns the maximum repeating# character in a given stringdef maxRepeating(str):     n = len(str)    count = 0    res = str[0]    cur_count = 1     # Traverse string except    # last character    for i in range(n):                 # If current character        # matches with next        if (i < n - 1 and            str[i] == str[i + 1]):            cur_count += 1         # If doesn't match, update result        # (if required) and reset count        else:            if cur_count > count:                count = cur_count                res = str[i]            cur_count = 1    return res # Driver codeif __name__ == "__main__":    str = "aaaabbaaccde"    print(maxRepeating(str)) # This code is contributed# by ChitraNayal
// C# program to find the maximum// consecutive repeating character// in given stringusing System; class GFG{ // function to find out the// maximum repeating character// in given stringstatic char maxRepeating(string str){    int n = str.Length;    int count = 0;    char res = str[0];    int cur_count = 1;     // Traverse string except    // last character    for (int i = 0; i < n; i++)    {        // If current character        // matches with next        if (i < n - 1 &&            str[i] == str[i + 1])            cur_count++;         // If doesn't match, update result        // (if required) and reset count        else        {            if (cur_count > count)            {                count = cur_count;                res = str[i];            }            cur_count = 1;        }    }    return res;} // Driver codepublic static void Main(){    string str = "aaaabbaaccde";    Console.Write(maxRepeating(str));}} // This code is contributed// by ChitraNayal
<?php// PHP program to find the maximum// consecutive repeating character// in given string // Returns the maximum repeating// character in a given stringfunction maxRepeating($str){    $n = strlen($str);    $count = 0;    $res = $str[0];    $cur_count = 1;     // Traverse string except    // last character    for ($i = 0; $i < $n; $i++)    {        // If current character        // matches with next        if ($i < $n - 1 &&            $str[$i] == $str[$i + 1])            $cur_count++;         // If doesn't match, update result        // (if required) and reset count        else        {            if ($cur_count > $count)            {                $count = $cur_count;                $res = $str[$i];            }            $cur_count = 1;        }    }     return $res;} // Driver code$str = "aaaabbaaccde";echo maxRepeating($str); // This code is contributed// by ChitraNayal?>
<script> // JavaScript program to find the maximum consecutive// repeating character in given string   // function to find out the maximum repeating  // character in given stringfunction maxRepeating( str){    var n = str.length;    var count = 0;    var res = str[0];    var cur_count = 1;     // Traverse string except last character    for (var i=0; i<n; i++)    {        // If current character matches with next        if (i < n-1 && str[i] == str[i+1])            cur_count++;         // If doesn't match, update result        // (if required) and reset 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>
Output:  
a
Time Complexity : O(n) Space Complexity : O(1)This article is contributed by DANISH_RAZA . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@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. 
jit_t
sudeepmukherjee
ukasp
rag2127
akshitsaxenaa09
Amazon
Strings
Amazon
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert string to char array in C++
Array of Strings in C++ (5 Different Ways to Create)
Longest Palindromic Substring | Set 1
Reverse words in a given string
Caesar Cipher in Cryptography
Length of the longest substring without repeating characters
Check whether two strings are anagram of each other
How to split a string in C/C++, Python and Java?
Remove duplicates from a given string
Top 50 String Coding Problems for Interviews