Decode the string encoded with the given algorithm
Last Updated :
31 May, 2021
Given a decoded string str which was decoded with the following encoding algorithm:
Write down the middle character of the string then delete it and repeat the process until there are no characters left. For example, "abba" will be encoded as "bbaa".
Note that the middle character is the first character of the two middle characters when the length of the string is even.
Examples:
Input: "ofrsgkeeeekgs"
Output: geeksforgeeks
Input: str = "bbaa"
Output: abba
Approach: It can be observed that while decoding the string, the first letter of the encoded string becomes the median of the decoded string. So first, write the very first character of the encoded string and remove it from the encoded string then start adding the first character of the encoded string first to the left and then to the right of the decoded string and do this task repeatedly till the encoded string becomes empty.
For example:
Encoded String Decoded String
ofrsgkeeeekgs o
frsgkeeeekgs fo
rsgkeeeekgs for
sgkeeeekgs sfor
gkeeeekgs sforg
keeeekgs ksforg
eeeekgs ksforge
eeekgs eksforge
eekgs eksforgee
ekgs eeksforgee
kgs eeksforgeek
gs geeksgorgeek
s geeksforgeeks
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to decode and print
// the original string
void decodeStr(string str, int len)
{
// To store the decoded string
char c[len] = "";
int med, pos = 1, k;
// Getting the mid element
if (len % 2 == 1)
med = len / 2;
else
med = len / 2 - 1;
// Storing the first element of the
// string at the median position
c[med] = str[0];
// If the length is even then store
// the second element also
if (len % 2 == 0)
c[med + 1] = str[1];
// k represents the number of characters
// that are already stored in the c[]
if (len & 1)
k = 1;
else
k = 2;
for (int i = k; i < len; i += 2) {
c[med - pos] = str[i];
// If string length is odd
if (len % 2 == 1)
c[med + pos] = str[i + 1];
// If it is even
else
c[med + pos + 1] = str[i + 1];
pos++;
}
// Print the decoded string
for (int i = 0; i < len; i++)
cout << c[i];
}
// Driver code
int main()
{
string str = "ofrsgkeeeekgs";
int len = str.length();
decodeStr(str, len);
return 0;
}
Java
// Java implementation of the approach
class GFG{
// Function to decode and print
// the original String
static void decodeStr(String str, int len)
{
// To store the decoded String
char []c = new char[len];
int med, pos = 1, k;
// Getting the mid element
if (len % 2 == 1)
med = len / 2;
else
med = len / 2 - 1;
// Storing the first element of the
// String at the median position
c[med] = str.charAt(0);
// If the length is even then store
// the second element also
if (len % 2 == 0)
c[med + 1] = str.charAt(1);
// k represents the number of characters
// that are already stored in the c[]
if (len % 2 == 1)
k = 1;
else
k = 2;
for(int i = k; i < len; i += 2)
{
c[med - pos] = str.charAt(i);
// If String length is odd
if (len % 2 == 1)
c[med + pos] = str.charAt(i + 1);
// If it is even
else
c[med + pos + 1] = str.charAt(i + 1);
pos++;
}
// Print the decoded String
for (int i = 0; i < len; i++)
System.out.print(c[i]);
}
// Driver code
public static void main(String[] args)
{
String str = "ofrsgkeeeekgs";
int len = str.length();
decodeStr(str, len);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the
# above approach
# Function to decode and print
# the original string
def decodeStr(str, len):
# To store the decoded string
c = ["" for i in range(len)]
pos = 1
# Getting the mid element
if(len % 2 == 1):
med = int(len / 2)
else:
med = int(len / 2 - 1)
# Storing the first element
# of the string at the
# median position
c[med] = str[0]
# If the length is even
# then store the second
# element also
if(len % 2 == 0):
c[med + 1] = str[1]
# k represents the number
# of characters that are
# already stored in the c[]
if(len & 1):
k = 1
else:
k = 2
for i in range(k, len, 2):
c[med - pos] = str[i]
# If string length is odd
if(len % 2 == 1):
c[med + pos] = str[i + 1]
# If it is even
else:
c[med + pos + 1] = str[i + 1]
pos += 1
# Print the decoded string
print(*c, sep = "")
# Driver code
str = "ofrsgkeeeekgs"
len = len(str)
decodeStr(str, len)
# This code is contributed by avanitrachhadiya2155
C#
// C# implementation of the approach
using System;
class GFG{
// Function to decode and print
// the original String
static void decodeStr(String str, int len)
{
// To store the decoded String
char []c = new char[len];
int med, pos = 1, k;
// Getting the mid element
if (len % 2 == 1)
med = len / 2;
else
med = len / 2 - 1;
// Storing the first element of the
// String at the median position
c[med] = str[0];
// If the length is even then store
// the second element also
if (len % 2 == 0)
c[med + 1] = str[1];
// k represents the number of characters
// that are already stored in the c[]
if (len % 2 == 1)
k = 1;
else
k = 2;
for(int i = k; i < len; i += 2)
{
c[med - pos] = str[i];
// If String length is odd
if (len % 2 == 1)
c[med + pos] = str[i + 1];
// If it is even
else
c[med + pos + 1] = str[i + 1];
pos++;
}
// Print the decoded String
for(int i = 0; i < len; i++)
Console.Write(c[i]);
}
// Driver code
public static void Main(String[] args)
{
String str = "ofrsgkeeeekgs";
int len = str.Length;
decodeStr(str, len);
}
}
// This code is contributed by sapnasingh4991
JavaScript
<script>
// JavaScript implementation of the approach
// Function to decode and print
// the original string
function decodeStr(str, len)
{
// To store the decoded string
var c = Array(len).fill("");
var med, pos = 1, k;
// Getting the mid element
if (len % 2 == 1)
med = parseInt(len / 2);
else
med = parseInt(len / 2) - 1;
// Storing the first element of the
// string at the median position
c[med] = str[0];
// If the length is even then store
// the second element also
if (len % 2 == 0)
c[med + 1] = str[1];
// k represents the number of characters
// that are already stored in the c[]
if (len & 1)
k = 1;
else
k = 2;
for (var i = k; i < len; i += 2) {
c[med - pos] = str[i];
// If string length is odd
if (len % 2 == 1)
c[med + pos] = str[i + 1];
// If it is even
else
c[med + pos + 1] = str[i + 1];
pos++;
}
// Print the decoded string
for (var i = 0; i < len; i++)
{
document.write(c[i]);
}
}
// Driver code
var str = "ofrsgkeeeekgs";
var len = str.length;
decodeStr(str, len);
</script>
The Complexity: O(n)
Similar Reads
Decrypt the String according to given algorithm Given encrypted string str consisting of alphabets and numeric characters, the task is to decrypt the string and find the encrypted message. In order to decrypt the message, find every cluster of numeric characters representing a single alphabetic character which can be obtained by calculating the m
7 min read
Program to find the kth character after decrypting a string Given a string str consisting of characters and numbers and an integer k, the task is to decrypt the string and the return the kth character in the decrypted string.In order to decrypt the string, traverse the string character by character and if the current character is an alphabet then append it t
8 min read
Decrypt the encoded string with help of Matrix as per given encryption decryption technique Given an encoded (or encrypted) string S of length N, an integer M. The task is to decrypt the encrypted string and print it. The encryption and decryption techniques are given as: Encryption: The original string is placed in a Matrix of M rows and N/M columns, such that the first character of the O
6 min read
Encrypt the given string with the following operations Given a string s, the task is to encrypt the string in the following way: If the frequency of current character is even, then increment current character by x.If the frequency of current character is odd, then decrement current character by x. Note: All the operations are circular that is adding 1 t
6 min read
Decode an Encoded Base 64 String to ASCII String Prerequisite : What is base64 Encoding and why we encode strings to base64 formatBase64 encoding is performed at sending node before transmitting bits over a network, and receiving node decodes that encoded data back to original ASCII string. Base64 character set is // 64 characterschar_set = "ABCDE
15+ min read