Open In App

Maximum consecutive repeating character in string

Last Updated : 18 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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: e
Explanation: character e comes 3 times consecutively which is maximum.

Input: s = “aaaabbcbbb”
Output: a
Explanation: character a comes 4 times consecutively which is maximum.

[Naive Approach] Using Nested Loop

The idea is to use a nested loop approach where the outer loop iterates through each character of the string, and for each character, the inner loop counts the consecutive occurrences of that character by moving forward until a different character is encountered.

C++
// C++ program to find the maximum consecutive
// repeating character in given string
#include<iostream>
using namespace std;

// function to find out the maximum repeating
// character in given string
char maxRepeating(string s) {
    int n = s.length();
    int maxCnt = 0;
    char res = s[0];
    
    // Find the maximum repeating character
    // starting from s[i]
    for (int i=0; i<n; i++) {
        int cnt = 0;
        for (int j=i; j<n; j++) {
            if (s[i] != s[j])
                break;
            cnt++;
        }

        // Update result if required
        if (cnt > maxCnt) {
            maxCnt = cnt;
            res = s[i];
        }
    }
    
    return res;
}

int main() {

    string s = "aaaabbaaccde";
    cout << maxRepeating(s);
    return 0;
}
Java
// Java program to find the maximum consecutive
// repeating character in given string
class GfG {

    // Function to find out the maximum repeating
    // character in given string
    static char maxRepeating(String s) {
        int n = s.length();
        int maxCnt = 0;
        char res = s.charAt(0);

        // Find the maximum repeating character
        // starting from s[i]
        for (int i = 0; i < n; i++) {
            int cnt = 0;
            for (int j = i; j < n; j++) {
                if (s.charAt(i) != s.charAt(j))
                    break;
                cnt++;
            }

            // Update result if required
            if (cnt > maxCnt) {
                maxCnt = cnt;
                res = s.charAt(i);
            }
        }

        return res;
    }

    public static void main(String[] args) {
        String s = "aaaabbaaccde";
        System.out.println(maxRepeating(s));
    }
}
Python
# Python program to find the maximum consecutive
# repeating character in given string

# Function to find out the maximum repeating
# character in given string
def maxRepeating(s):
    n = len(s)
    maxCnt = 0
    res = s[0]

    # Find the maximum repeating character
    # starting from s[i]
    for i in range(n):
        cnt = 0
        for j in range(i, n):
            if s[i] != s[j]:
                break
            cnt += 1

        # Update result if required
        if cnt > maxCnt:
            maxCnt = cnt
            res = s[i]

    return res

if __name__ == "__main__":
    s = "aaaabbaaccde"
    print(maxRepeating(s))
C#
// C# program to find the maximum consecutive
// repeating character in given string
using System;

class GfG {

    // Function to find out the maximum repeating
    // character in given string
    static char maxRepeating(string s) {
        int n = s.Length;
        int maxCnt = 0;
        char res = s[0];

        // Find the maximum repeating character
        // starting from s[i]
        for (int i = 0; i < n; i++) {
            int cnt = 0;
            for (int j = i; j < n; j++) {
                if (s[i] != s[j])
                    break;
                cnt++;
            }

            // Update result if required
            if (cnt > maxCnt) {
                maxCnt = cnt;
                res = s[i];
            }
        }

        return res;
    }

    static void Main(string[] args) {
        string s = "aaaabbaaccde";
        Console.WriteLine(maxRepeating(s));
    }
}
JavaScript
// 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 n = s.length;
    let maxCnt = 0;
    let res = s[0];

    // Find the maximum repeating character
    // starting from s[i]
    for (let i = 0; i < n; i++) {
        let cnt = 0;
        for (let j = i; j < n; j++) {
            if (s[i] !== s[j])
                break;
            cnt++;
        }

        // Update result if required
        if (cnt > maxCnt) {
            maxCnt = cnt;
            res = s[i];
        }
    }

    return res;
}

//Driver Code
let s = "aaaabbaaccde";
console.log(maxRepeating(s));

Output: 

a

Time Complexity : O(n^2), as we are using two nested loops.
Space Complexity : O(1)

[Expected Approach 1] Optimised Nested Loops

In the above approach, instead of running outer loop for each index from 0 to n -1, we can update it from the ending point of the inner loop, as we need not to count same elements multiple times, and can start the next iteration from the different element.

C++
// C++ program to find the maximum consecutive
// repeating character in given string
#include<iostream>
using namespace std;

// function to find out the maximum repeating
// character in given string
char maxRepeating(string s) {
    int n = s.length();
    int maxCnt = 0;
    char res = s[0];
    
    // Find the maximum repeating character
    // starting from s[i]
    for (int i=0; i<n; i++) {
        int cnt = 1;
        while(i + 1 < n && s[i] == s[i + 1]) {
            i++;
            cnt++;
        }

        // Update result if required
        if (cnt > maxCnt) {
            maxCnt = cnt;
            res = s[i];
        }
    }
    
    return res;
}

int main() {

    string s = "aaaabbaaccde";
    cout << maxRepeating(s);
    return 0;
}
Java
// Java program to find the maximum consecutive
// repeating character in given string
class GFG {

    // function to find out the maximum repeating
    // character in given string
    static char maxRepeating(String s) {
        int n = s.length();
        int maxCnt = 0;
        char res = s.charAt(0);

        // Find the maximum repeating character
        // starting from s[i]
        for (int i = 0; i < n; i++) {
            int cnt = 1;
            while (i + 1 < n && s.charAt(i) == s.charAt(i + 1)) {
                i++;
                cnt++;
            }

            // Update result if required
            if (cnt > maxCnt) {
                maxCnt = cnt;
                res = s.charAt(i);
            }
        }

        return res;
    }

    public static void main(String[] args) {
        String s = "aaaabbaaccde";
        System.out.println(maxRepeating(s));
    }
}
Python
# Python program to find the maximum consecutive
# repeating character in given string

# function to find out the maximum repeating
# character in given string
def maxRepeating(s):
    n = len(s)
    maxCnt = 0
    res = s[0]

    # Find the maximum repeating character
    # starting from s[i]
    i = 0
    while i < n:
        cnt = 1
        while i + 1 < n and s[i] == s[i + 1]:
            i += 1
            cnt += 1

        # Update result if required
        if cnt > maxCnt:
            maxCnt = cnt
            res = s[i]

        i += 1

    return res

# Main function
def main():
    s = "aaaabbaaccde"
    print(maxRepeating(s))

if __name__ == "__main__":
    main()
C#
// C# program to find the maximum consecutive
// repeating character in given string
using System;

class GFG {

    // function to find out the maximum repeating
    // character in given string
    static char MaxRepeating(string s) {
        int n = s.Length;
        int maxCnt = 0;
        char res = s[0];

        // Find the maximum repeating character
        // starting from s[i]
        for (int i = 0; i < n; i++) {
            int cnt = 1;
            while (i + 1 < n && s[i] == s[i + 1]) {
                i++;
                cnt++;
            }

            // Update result if required
            if (cnt > maxCnt) {
                maxCnt = cnt;
                res = s[i];
            }
        }

        return res;
    }

    public static void Main() {
        string s = "aaaabbaaccde";
        Console.WriteLine(MaxRepeating(s));
    }
}
JavaScript
// 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(s) {
    let n = s.length;
    let maxCnt = 0;
    let res = s[0];

    // Find the maximum repeating character
    // starting from s[i]
    for (let i = 0; i < n; i++) {
        let cnt = 1;
        while (i + 1 < n && s[i] === s[i + 1]) {
            i++;
            cnt++;
        }

        // Update result if required
        if (cnt > maxCnt) {
            maxCnt = cnt;
            res = s[i];
        }
    }

    return res;
}

// Main function
function main() {
    let s = "aaaabbaaccde";
    console.log(maxRepeating(s));
}

// Run the main function
main();

Output
a

Time Complexity: O(n)
Auxiliary Space: O(1)

[Expected Approach 2] Using Counter Variable

The idea is to traverse the string from the second character (index 1) and for each character, compare it with the previous character to identify consecutive repeating sequences. When the current character matches the previous one, we increment a counter, and when it differs, reset the counter to 1. Check if the current streak is longer than the maximum streak seen so far – if yes, we update both the maximum count and the result character.

C++
// C++ program to find the maximum consecutive
// repeating character in given string
#include <iostream>
using namespace std;

// Function to find out the maximum repeating
// character in given string
char maxRepeating(string s) {
    int n = s.length();
    int maxCnt = 0;
    char res = s[0];
    int cnt = 1;

    for (int i = 1; i < n; i++) {

        // If current char matches with
        // previous, increment cnt
        if (s[i] == s[i - 1]) {
            cnt++;
        }
        else {

            // Reset cnt
            cnt = 1;
        }

        // Update result if required
        if (cnt > maxCnt) {
            maxCnt = cnt;
            res = s[i - 1];
        }
    }

    return res;
}

// Driver Code
int main() {
    string s = "aaaabbaaccde";
    cout << maxRepeating(s) << endl;
    return 0;
}
Java
// Java program to find the maximum consecutive
// repeating character in given string
class GFG {

    // Function to find out the maximum repeating
    // character in given string
    static char maxRepeating(String s) {
        int n = s.length();
        int maxCnt = 0;
        char res = s.charAt(0);
        int cnt = 1;

        for (int i = 1; i < n; i++) {

            // If current char matches with
            // previous, increment cnt
            if (s.charAt(i) == s.charAt(i - 1)) {
                cnt++;
            } 
            else {

                // Reset cnt
                cnt = 1;
            }

            // Update result if required
            if (cnt > maxCnt) {
                maxCnt = cnt;
                res = s.charAt(i - 1);
            }
        }

        return res;
    }

    // Driver Code
    public static void main(String[] args) {
        String s = "aaaabbaaccde";
        System.out.println(maxRepeating(s));
    }
}
Python
# Python program to find the maximum consecutive
# repeating character in given string

# Function to find out the maximum repeating
# character in given string
def maxRepeating(s):
    n = len(s)
    maxCnt = 0
    res = s[0]
    cnt = 1

    for i in range(1, n):

        # If current char matches with
        # previous, increment cnt
        if s[i] == s[i - 1]:
            cnt += 1
        else:

            # Reset cnt
            cnt = 1

        # Update result if required
        if cnt > maxCnt:
            maxCnt = cnt
            res = s[i - 1]

    return res

# Driver Code
s = "aaaabbaaccde"
print(maxRepeating(s))
C#
// C# program to find the maximum consecutive
// repeating character in given string
using System;

class GFG {

    // Function to find out the maximum repeating
    // character in given string
    static char MaxRepeating(string s) {
        int n = s.Length;
        int maxCnt = 0;
        char res = s[0];
        int cnt = 1;

        for (int i = 1; i < n; i++) {

            // If current char matches with
            // previous, increment cnt
            if (s[i] == s[i - 1]) {
                cnt++;
            } 
            else {

                // Reset cnt
                cnt = 1;
            }

            // Update result if required
            if (cnt > maxCnt) {
                maxCnt = cnt;
                res = s[i - 1];
            }
        }

        return res;
    }

    // Driver Code
    public static void Main() {
        string s = "aaaabbaaccde";
        Console.WriteLine(MaxRepeating(s));
    }
}
JavaScript
// 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(s) {
    let n = s.length;
    let maxCnt = 0;
    let res = s[0];
    let cnt = 1;

    for (let i = 1; i < n; i++) {

        // If current char matches with
        // previous, increment cnt
        if (s[i] === s[i - 1]) {
            cnt++;
        }
        else {

            // Reset cnt
            cnt = 1;
        }

        // Update result if required
        if (cnt > maxCnt) {
            maxCnt = cnt;
            res = s[i - 1];
        }
    }

    return res;
}

//Driver Code
let s = "aaaabbaaccde";
console.log(maxRepeating(s));

Output
a

Time Complexity: O(n)
Auxiliary Space: O(1)



Next Article
Article Tags :
Practice Tags :

Similar Reads