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++
#include <iostream>
using namespace std;
void concesStr(string str)
{
int lenStr = str.length();
for ( int i = 0; i < lenStr; i++) {
int curr_count = 1;
for ( int j = i + 1; j < lenStr;
j++) {
if (str[i] != str[j]) {
break ;
}
curr_count++;
if (curr_count > 2) {
i++;
}
}
if (curr_count > 1) {
cout << str[i] << ": "
<< curr_count << endl;
}
}
}
int main()
{
string str = "Hellolllee" ;
concesStr(str);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void consecStr(String str)
{
int lenStr = str.length();
for ( int i = 0 ; i < lenStr; i++) {
int curr_count = 1 ;
for ( int j = i + 1 ; j < lenStr;
j++) {
if (str.charAt(i) != str.charAt(j)) {
break ;
}
curr_count++;
if (curr_count > 2 ) {
i++;
}
}
if (curr_count > 1 ) {
System.out.print(str.charAt(i)
+ ": " + curr_count
+ "\n" );
}
}
}
public static void main(String[] args)
{
consecStr( "Hellolllee" );
}
}
|
Python3
def consecStr( str ):
lenStr = len ( str );
i = 0 ;
for k in range (lenStr):
curr_count = 1 ;
for j in range (i + 1 ,lenStr):
if ( str [i] ! = str [j]):
break ;
curr_count + = 1 ;
if (curr_count > 2 ):
i + = 1 ;
if (curr_count > 1 ):
print ( str [i] , ": " , curr_count , "");
i + = 1 ;
if __name__ = = '__main__' :
consecStr( "Hellolllee" );
|
C#
using System;
class GFG
{
static void concesStr( string str)
{
int lenStr = str.Length;
for ( int i = 0; i < lenStr; i++)
{
int curr_count = 1;
for ( int j = i + 1; j < lenStr;
j++)
{
if (str[i] != str[j])
{
break ;
}
curr_count++;
if (curr_count > 2)
{
i++;
}
}
if (curr_count > 1)
{
Console.WriteLine(str[i] + ": " + curr_count);
}
}
}
public static void Main()
{
string str = "Hellolllee" ;
concesStr(str);
}
}
|
Javascript
<script>
function concesStr(str) {
let lenStr = str.length;
for (let i = 0; i < lenStr; i++) {
let curr_count = 1;
for (let j = i + 1; j < lenStr;
j++) {
if (str[i] != str[j]) {
break ;
}
curr_count++;
if (curr_count > 2) {
i++;
}
}
if (curr_count > 1) {
document.write(str[i] + ": "
+ curr_count + '<br>' );
}
}
}
let str = "Hellolllee" ;
concesStr(str);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
31 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...