Decode a given string by removing duplicate occurrences
Given encoded string str such that ‘a’ is written 1 time, ‘b’ is written 2 times, and so on, until ‘z’ is written 26 times, the task is to decode the given string str.
Note: The letter may contain spaces and punctuation marks, so don’t ignore those.
Examples:
Input: str = “bbadddd”
Output: bad
Explanation:
As each letter is written corresponding to the number of appearances in the English Alphabets. Therefore, the resultant string is “bad”.
Input: str = “abbbbacccdddd”
Output: abbacd
Approach: The given problem can be solved by iterating the given string str, and for each character and push that character in an output string and move that respective position ahead to check for the other character. Follow the steps below to solve the problem:
- Initialize a variable, say output as an empty string that stores the resultant string.
- Define a function findRepetition(char c) and perform the following steps:
- If the value of c is in the range from a to z, then return the value of c-‘a’.
- Otherwise, if the value of c is in the range from A to Z, then return the value of c-‘Z’.
- Otherwise, return 0.
- Iterate over a range [0, N] using the variable i and perform the following steps:
- Push the ith character of the string str into the string output.
- Call the function findRepetition(str[i]) to count the number of steps ith index has to move ahead.
- After performing the above steps, print the string output as the resultant string.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findRepitition( char a)
{
if (a <= 'z' && a >= 'a' ) {
return a - 'a' ;
}
else if (a <= 'Z' && a >= 'A' ) {
return a - 'A' ;
}
return 0;
}
void decodeString(string str)
{
string output = "" ;
for ( int i = 0; i < str.length(); i++) {
output.push_back(str[i]);
i += findRepitition(str[i]);
}
cout << "Decrypted code is {"
<< output << "}" << endl;
}
int main()
{
string str = "abbbb acccdddd" ;
decodeString(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findRepitition( char a)
{
if (a <= 'z' && a >= 'a' )
{
return a - 'a' ;
}
else if (a <= 'Z' && a >= 'A' )
{
return a - 'A' ;
}
return 0 ;
}
static void decodeString(String str)
{
String output = "" ;
for ( int i = 0 ; i < str.length(); i++)
{
output += (str.charAt(i));
i += findRepitition(str.charAt(i));
}
System.out.print( "Decrypted code is {" +
output + "}" + "\n" );
}
public static void main(String[] args)
{
String str = "abbbb acccdddd" ;
decodeString(str);
}
}
|
Python3
def findRepetition(a):
if a < = 'z' and a > = 'a' :
return ord (a) - 97
elif a < = 'Z' and a > = 'A' :
return ord (a) - 65
else :
return 0
def decodeString(str_):
output = ""
i = 0
while (i < len (str_)):
output + = str_[i]
i + = findRepetition(str_[i]) + 1
print ( "Decrypted code is {" + output + "}" )
str_ = "abbbb acccdddd"
decodeString(str_)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findRepitition( char a)
{
if (a <= 'z' && a >= 'a' ) {
return ( int )a - 97;
}
else if (a <= 'Z' && a >= 'A' ) {
return ( int )a - 65;
}
return 0;
}
static void decodeString( string str)
{
string output = "" ;
for ( int i = 0; i < str.Length; i++) {
output += str[i];
i += findRepitition(str[i]);
}
Console.Write( "Decrypted code is {" + output + "}" );
}
public static void Main()
{
string str = "abbbb acccdddd" ;
decodeString(str);
}
}
|
Javascript
<script>
function findRepitition(a)
{
if (a.charCodeAt(0) <= 'z' .charCodeAt(0) && a.charCodeAt(0) >= 'a' .charCodeAt(0)) {
return a.charCodeAt(0) - 'a' .charCodeAt(0);
}
else if (a <= 'Z' && a >= 'A' ) {
return a.charCodeAt(0) - 'A' .charCodeAt(0);
}
return 0;
}
function decodeString(str) {
let output = "" ;
for (let i = 0; i < str.length; i++) {
output += (str[i]);
i += findRepitition(str[i]);
}
document.write( "Decrypted code is {"
+ output + "}" + "<br>" );
}
let str = "abbbb acccdddd" ;
decodeString(str);
</script>
|
Output:
Decrypted code is {abb acd}
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
02 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...