Print the frequency of adjacent repeating characters in given string Last Updated : 31 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like 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: 2Explanation: Consecutive Repeating Character from the given string is "l" and its frequency is 2. Input: str = "Hellolllee"Output: l: 2 l: 3 e: 2Explanation: 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> Outputl: 2 l: 3 e: 2 Time Complexity: O(N2)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article First non-repeating character in a stream S sourabhnaikssj Follow Improve Article Tags : Strings Greedy Algo Geek DSA Algo-Geek 2021 frequency-counting +2 More Practice Tags : GreedyStrings Similar Reads Maximum repeated frequency of characters in a given string Given a string S, the task is to find the count of maximum repeated frequency of characters in the given string S.Examples: Input: S = "geeksgeeks" Output: Frequency 2 is repeated 3 times Explanation: Frequency of characters in the given string - {"g": 2, "e": 4, "k": 2, "s": 2} The frequency 2 is r 6 min read Find the last non repeating character in string Given a string str, the task is to find the last non-repeating character in it. For example, if the input string is "GeeksForGeeks", then the output should be 'r' and if the input string is "GeeksQuiz" then the output should be 'z'. if there is no non-repeating character then print -1.Examples: Inpu 5 min read First non-repeating character in a stream Given an input stream s consisting solely of lowercase letters, you are required to identify which character has appeared only once in the stream up to each point. If there are multiple characters that have appeared only once, return the one that first appeared. If no character has appeared only onc 15+ min read Queries to find the last non-repeating character in the sub-string of a given string Given a string str, the task is to answer Q queries where every query consists of two integers L and R and we have to find the last non-repeating character in the sub-string str[L...R]. If there is no non-repeating character then print -1.Examples: Input: str = "GeeksForGeeks", q[] = {{2, 9}, {2, 3} 11 min read Maximum repeating character for every index in given String Given string str consisting of lowercase alphabets, the task is to find the maximum repeating character obtained for every character of the string. If for any index, more than one character has occurred a maximum number of times, then print the character which had occurred most recently. Examples: I 6 min read Print all the duplicate characters in a string Given a string s, the task is to identify all characters that appear more than once and print each as a list containing the character and its count. Examples:Input: s = "geeksforgeeks"Output: ['e', 4], ['g', 2], ['k', 2], ['s', 2]Explanation: Characters e, g, k, and s appear more than once. Their co 8 min read Like