Decode a given string by removing duplicate occurrences


The aim of this article is to implement a program to decode a given string by removing duplicate occurrences.

As you are aware of what a string is, a string is nothing but a collection of characters. Also there is no restriction in the repetitions of characters in a string. A string can have the same character occurring multiple times. In this we will figure out a way to decode a given encoded string str by removing duplicate occurrences.

The objective would be to decode the provided string str, which has been encoded with 'a' appearing just once, 'b' twice, 'c' three times, 'd' four times, and continued until 'z' appears 26 times.

Problem Statement

Implement a program decode a given string by removing duplicate occurrences.

Note − Don’t ignore the spaces which the letter may contain.

Sample Example 1

Let us take the input string str = “abbbb accc”
The output obtained is: abb ac

Explanation

Each letter is written with the number of times it appears in the English Alphabet in mind. The resultant string is "abb acc" because here the letter b is repeated four times. The letter a is repeated two times and finally the letter c is repeated 3 times.

Also in this case, the space is not ignored.

Sample Example 2

Let us take the input string str = “ddddadddd”
The output obtained is: dad

Explanation

Each letter is written with the number of times it appears in the English Alphabet in mind. The resultant string is "dad" because here the letter d is repeated eight times and finally the letter a is present only one time.

Also in this case, there are no spaces between the characters.

Sample Example 3

Let us take the input string str = “abbccc”
The output obtained is: abc

Explanation

Each letter is written with the number of times it appears in the English Alphabet in mind. The resultant string is "abc'' because here the letter a is present only one time. The letter b is repeated two times and finally the letter c is repeated three times.

Also in this case, there are no spaces between the characters.

Approach

In order to decode a given string by removing duplicate occurrences, we apply the following methodology in our article.

The approach to solve this problem and to decode a given string by removing duplicate occurrences is based on iterating the string.

That is, the stated problem can be resolved by iterating the string str and pushing each character into the output string before moving that position forward to look for the next character.

Algorithm

The algorithm to print the number of camel case character present in a given string is given below

To solve the issue, adhere to the instructions listed below −

  • Step 1 − Start

  • Step 2 − Define string

  • Step 3 − Create a variable called result with the initial value of an empty string to store the output string.

  • Step 4 − Create the function findOccurences(char a1) and carry out the subsequent actions −

  • Step 5 − Return the value of a1 as 'a' if the value of a1 falls between a and z. Return the value of a1 as 'Z' if the range of values for a1 is something besides A to Z. If not, return 0.

  • Step 6 − Define function decodeTheString(string s) to decode the string s

  • Step 7 − Print the string result to be the final string after completing the aforementioned stages.

  • Step 8 − Stop

Example: C++ Program

Here is the C++ program implementation of the above written algorithm to decode a given string by removing duplicate occurrences

// C++ program for our above algorithm
#include <bits/stdc++.h>
using namespace std;

// Function to count the number of  occurences of each character
int findOccurences(char a1){

   // If the character is a lower case , that is [a-z]
   if (a1 <= 'z' && a1 >= 'a') {
      return a1 - 'a';
   }
   
   // If the character is an uppercase, that is [A-Z]
   else if (a1 <= 'Z' && a1 >= 'A') {
      return a1 - 'A';
   }
   
   // If the character is something else  like a punctuation mark then
   return 0;
}

// Function used for decoding the given string str
void decodeTheString(string s){
   string result = "";
   
   // Iterate through the provided string str
   for (int i = 0; i < s.length(); i++) {
      result.push_back(s[i]);
      
      // Find the index i of the next characterto be printed
      i += findOccurences(s[i]);
   }
   cout << "The decoded string: " << result << endl;
}
int main(){
   string s = "aaabbbb";
   cout << "Input string: "<< s << endl;
   decodeTheString(s);
   return 0;
}

Output

Input string: aaabbbb
The decoded string: aaabb

Conclusion

Likewise, we can decode any given string by removing the duplicate occurrences from it.

The challenge of decoding any given string by removing the duplicate occurrences from it is resolved in this article. Here C++ programming code as well as the algorithm to decode any given string by removing the duplicate occurrences from it is provided.

Updated on: 28-Jul-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements