Sunday, October 12, 2025
HomeData Modelling & AIPrint the frequency of adjacent repeating characters in given string

Print the frequency of adjacent repeating characters in given string

Given a string str of length N. The task is to print the frequency of adjacent repeating characters. 

Examples:

Input: str = “Hello”
Output: l: 2
Explanation: Consecutive Repeating Character from the given string is “l” and its frequency is 2.

Input: str = “Hellolllee”
Output: l: 2
              l: 3
              e: 2
Explanation: Consecutive Repeating Characters from the given string are “l, “, “l” and “e” 
and its frequencies are as follows: 2, 3, 2.

 

Approach: This problem can be solved simply by traversing and keeping track of adjacent repeating characters. Follow the steps below to solve the given problem.

  • Iterate from i = 0 till string length.
  • Maintain a counter.
  • Again iterate via the next loop from i+1 till string length.
  • The counter will increment until the next character is different.
  • For characters having more than 2 frequencies increment i so that the count will remain intact.
  • If the counter is greater than 1 then print.

Below is the implementation of the above approach:

C++




// C++ program for above approach
#include <iostream>
using namespace std;
 
// Function to find frequency
// of repeating characters
void concesStr(string str)
{
 
    // Length of string
    int lenStr = str.length();
 
    // Iterate through 1st pointer
    for (int i = 0; i < lenStr; i++) {
 
        // Keep a counter
        int curr_count = 1;
 
        // Iterate through 2nd pointer
        for (int j = i + 1; j < lenStr;
             j++) {
 
            // if next element is different
            // then break
            if (str[i] != str[j]) {
                break;
            }
            curr_count++;
 
            // Example: if count is 3
            // then move the first
            // pointer so that
            // count remains intact
            if (curr_count > 2) {
                i++;
            }
        }
 
        // Condition for print more than 1
        // count characters
        if (curr_count > 1) {
            cout << str[i] << ": "
                 << curr_count << endl;
        }
    }
}
 
// Driver Code
int main()
{
    string str = "Hellolllee";
 
    concesStr(str);
    return 0;
}


Java




// Java code to implement above approach
import java.io.*;
 
class GFG {
 
    // Function to find frequency
    // of repeating characters
    public static void consecStr(String str)
    {
        int lenStr = str.length();
 
        // Iterate through 1st pointer
        for (int i = 0; i < lenStr; i++) {
 
            // keep a counter
            int curr_count = 1;
 
            // Iterate through 2nd pointer
            for (int j = i + 1; j < lenStr;
                 j++) {
 
                // if next element is different
                // then break
                if (str.charAt(i) != str.charAt(j)) {
                    break;
                }
                curr_count++;
 
                // Example: if count is 3 then
                // move the first pointer
                // so that count remains intact
                if (curr_count > 2) {
                    i++;
                }
            }
 
            // Condition for print
            // more than 1 count characters
            if (curr_count > 1) {
                System.out.print(str.charAt(i)
                                 + ": " + curr_count
                                 + "\n");
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        consecStr("Hellolllee");
    }
}


Python3




# Python code to implement above approach
 
# Function to find frequency
# of repeating characters
def consecStr(str):
    lenStr = len(str);
    i = 0;
     
    # Iterate through 1st pointer
    for k in range(lenStr):
 
        # keep a counter
        curr_count = 1;
 
        # Iterate through 2nd pointer
        for j in range(i+1,lenStr):
 
            # if next element is different
            # then break
            if (str[i] != str[j]):
                break;
 
            curr_count += 1;
 
            # Example: if count is 3 then
            # move the first pointer
            # so that count remains intact
            if (curr_count > 2):
                i += 1;
 
        # Condition for print
        # more than 1 count characters
        if (curr_count > 1):
            print(str[i] , ": " , curr_count , "");
        i += 1;
 
# Driver code
if __name__ == '__main__':
    consecStr("Hellolllee");
 
# This code is contributed by 29AjayKumar


C#




// C# program for above approach
using System;
 
class GFG
{
  // Function to find frequency
  // of repeating characters
  static void concesStr(string str)
  {
 
    // Length of string
    int lenStr = str.Length;
 
    // Iterate through 1st pointer
    for (int i = 0; i < lenStr; i++)
    {
 
      // Keep a counter
      int curr_count = 1;
 
      // Iterate through 2nd pointer
      for (int j = i + 1; j < lenStr;
           j++)
      {
 
        // if next element is different
        // then break
        if (str[i] != str[j])
        {
          break;
        }
        curr_count++;
 
        // Example: if count is 3
        // then move the first
        // pointer so that
        // count remains intact
        if (curr_count > 2)
        {
          i++;
        }
      }
 
      // Condition for print more than 1
      // count characters
      if (curr_count > 1)
      {
        Console.WriteLine(str[i] + ": " + curr_count);
      }
    }
  }
 
  // Driver Code
  public static void Main()
  {
    string str = "Hellolllee";
 
    concesStr(str);
  }
}
 
// This code is contributed by gfgking.


Javascript




<script>
        // JavaScript code for the above approach
 
        // Function to find frequency
        // of repeating characters
        function concesStr(str) {
 
            // Length of string
            let lenStr = str.length;
 
            // Iterate through 1st pointer
            for (let i = 0; i < lenStr; i++) {
 
                // Keep a counter
                let curr_count = 1;
 
                // Iterate through 2nd pointer
                for (let j = i + 1; j < lenStr;
                    j++) {
 
                    // if next element is different
                    // then break
                    if (str[i] != str[j]) {
                        break;
                    }
                    curr_count++;
 
                    // Example: if count is 3
                    // then move the first
                    // pointer so that
                    // count remains intact
                    if (curr_count > 2) {
                        i++;
                    }
                }
 
                // Condition for print more than 1
                // count characters
                if (curr_count > 1) {
                    document.write(str[i] + ": "
                        + curr_count + '<br>');
                }
            }
        }
 
        // Driver Code
        let str = "Hellolllee";
        concesStr(str);
 
       // This code is contributed by Potta Lokesh
    </script>


 
 

Output

l: 2
l: 3
e: 2

 

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

 

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 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11885 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6840 POSTS0 COMMENTS
Ted Musemwa
7105 POSTS0 COMMENTS
Thapelo Manthata
6795 POSTS0 COMMENTS
Umr Jansen
6795 POSTS0 COMMENTS