Open In App

Print the frequency of adjacent repeating characters in given string

Improve
Improve
Like Article
Like
Save
Share
Report

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) 

 



Last Updated : 31 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads