Print the frequency of adjacent repeating characters in a given string


A string is a data structure consisting of a sequence of characters. The end of the string is marked by a special character, called a null character, which is usually represented by the ASCII code 0.

Problem Statement

Given a string s of a certain length, the task at hand is to print adjacent repeating characters along with the frequency of their repetition.

For Example

Input: s = “committee”
Output: [[m, 2], [t, 2], [e, 2]]

Explanation

The character m occurs consecutively twice. Similarly the character t and e also occur twice consecutively. Therefore we return the vector of pairs with these characters and their respective frequency of repetition.

Input: s = “kkbkkk”
Output: [[k,2], [k,3]]

Explanation

The character k occurs twice consecutively so we add it to the answer vector. It is followed by the character b, which does not occur repeatedly, so we do not include it in our answer. Furthermore, the character k repeats itself again so we append it to the answer again along with the new frequency of repetition.

Input: s = “abcdae”
Output: []

Explanation

None of the characters in the string s repeat consecutively. Hence we return an empty vector of pairs as our final answer.

Solution Approach

The problem statement is quite straightforward and the solution approach follows in its footsteps. The idea is to simply traverse the given string and check if the next character is the same as the present character.

If that is the case, update the current frequency of the character by 1. Continue this process till we reach a new character. At that point we simply check if the previous character repeated itself and if so, we add the pair consisting of the character and its frequency to our answer vector of pairs.

Else if the character does not repeat itself, we simply iterate over to the next character and follow the same procedure.

This solution works in linear time. It can be better understood through the pseudocode and algorithm provided below.

Pseudocode

  • Declare a vector of pairs ‘ans’ to store the final result.

  • Initialize an integer variable ‘curr_freq’ which will store the current frequency of repetition of a character.

  • Declare a character variable ‘prev_ele’ which will store the element before the current element of the string whose frequency we are calculating.

  • Iterate over the string

    • if the prev_ele == current element, increment curr_freq

    • else

      • If curr_freq > 1, append {prev_ele, curr_freq} to ans

      • Reset curr_freq

      • Update prev_ele

  • Return ans

Algorithm

  • Function repeating_element()

    • initialize curr_freq = 1

    • initialize prev_ele = s[0]

    • for (i : 1 to n - 1)

      • if (s[i] == prev_ele)

        • curr_freq ++

      • else

        • if (curr_freq > 1)

          • ans.push_back({prev_ele, curr_freq})

        • prev_ele = s[i]

        • curr_freq = 1

    • if (curr_freq > 1)

      • ans.push_back({prev_ele, curr_freq})

    • return ans

Function main()

  • Initialize string s

  • Declare vector<pair<char,int>>ans

  • if (s.length() == 0) return 0;

  • Function call repeating element(s, ans)

  • Print ans

Example: C++ Program Code

The following c++ program returns a vector of pairs of all the elements that occur more than once consecutively in the string along with their frequency of repetition. It declares a vector of pairs of char and int to store the final answer. We make a function call repeating_element() and pass the string and ans vector by reference to it.

The function finds and stores the elements which repeat themselves consecutively using 2 variables, curr_freq and prev_ele. Then we simply print the ans vector.

// C++ program to print the characters of a given string which repeat themselves consecutively along with the frequency of their repetition
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// the function adds the pair of repeating element and its freq to ans vector
void repeating_element(string & s, vector<pair<char, int>> & ans){
   int curr_freq = 1;
   char prev_ele = s[0];
   // loop to iterate over all the characters in the string and add pair of repeating characters and their frequency to ans vector
   for (int i = 1; i < s.length(); i++){
      // if current character matches prev_ele, increment its frequency of occurrence
      if (s[i] == prev_ele){
      curr_freq++;
      }
      else{
         // if the frequency of previous element is greater than 1, add it to ans vector
         if (curr_freq > 1){
            ans.push_back(make_pair(prev_ele, curr_freq));
         }
         // reset the curr_freq to 1 and update the previous element to current element
         curr_freq = 1;
         prev_ele = s[i];
      }
   }
   // to check if there is a repeating character at the end
   if (curr_freq > 1){
      ans.push_back(make_pair(prev_ele, curr_freq));
   }
   return;
}

int main(){
   string s = "committee";
   vector<pair<char, int>> ans;
   if (s.length() == 0)
   return 0;
   repeating_element(s, ans);
   for (auto & x : ans){
      cout << x.first << " : " << x.second;
      cout << endl;
   }
   return 0;
}

Output

m : 2
t : 2
e : 2

Time and Space Complexity Analysis

Time Complexity − O(n)

  • repeating_element function −

  • A for loop iterates over the characters of the input string s. Let n be the length of the string. The loop iterates from index 1 to n-1, so it performs n-1 iterations. Each iteration performs constant time operations (comparisons, increments, and pushing to a vector). Therefore, the time complexity of this loop is O(n).

  • main function −

  • A for loop iterates over the elements in the ans vector to print them. The number of iterations in this loop depends on the number of repeating characters found in the input string. In the worst case, if all characters in the string are repeating,which means the loop iterates n/2 times. Hence, the time complexity of this loop is O(n/2)

    .

Overall, the time complexity of the code is O(n + n/2) = O(n).

Space Complexity − O(n)

  • ans vector −

  • The vector stores pairs of repeating characters and their frequencies. In the worst case, if all characters in the string are unique, then the vector could have a size of n/2, as each repeating character is stored once. Hence, the space complexity of the ans vector is O(n).

Therefore, the space complexity of the code is O(n).

Conclusion

The article discusses a simple linear time and space complexity solution to print the frequency of adjacent repeating characters in a given string. We store the result in a vector of pairs and print that. The article discusses the problem statement with the help of various examples. Additionally we discuss the solution approach with the help of pseudocode, algorithm and the actual c++ program code. At the end we discuss the time and space complexity in depth to gain a complete understanding of the solution.

Updated on: 25-Oct-2023

41 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements