Decode a median string to the original string

Given a string s written in median form, change it back to the original string. Median letter in a string is the letter which is in the middle of the string. If the string’s length is even, the median letter is the left of the two middle letters. The given string is formed by writing down the median letter of the word, then deleting it and repeating the process until there are no letters left.

Examples:

Input: eekgs
Output: geeks 
Explanation: in the original string “geeks” 
can be written in median form by picking up 
e first then, again e, then k then g and at
the end s. As these are the median when the
median letter is picked and deleted. 

Input: abc 
Output: bac 
Explanation: median of bac is a, then median
of bc is b, then median of c is c. 

To find the answer we can iterate through the given encoded string from left to right and add each letter in the answer string, one letter to the begin, next letter to the end, next letter to begin and so on. If n is even than the first letter must be added to the begin and the second letter to the end. In the other case, the first letter to the end, second to the begin. We need to make it until we do not add all letters from the given string.
Note: For strings with even length, when we add first character to begin and second character to end then the remaining string will always be of even length. Same is true for strings with odd lengths.

Given below is the implementation of the above approach

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to decode a median string
// to the original string
  
#include <bits/stdc++.h>
using namespace std;
  
// function to calculate the median back string
string decodeMedianString(string s)
{
    // length of string
    int l = s.length();
  
    // initialize a blank string
    string s1 = "";
  
    // Flag to check if length is even or odd
    bool isEven = (l % 2 == 0)? true : false;
  
    // traverse from first to last
    for (int i = 0; i < l; i += 2) {
  
        // if len is even then add first character
        // to beginning of new string and second
        // character to end
        if (isEven) {   
            s1 = s[i] + s1;
            s1 += s[i + 1];
        } else {
  
            // if current length is odd and is
            // greater than 1
            if (l - i > 1) {
  
                // add first character to end and
                // second character to beginning
                s1 += s[i];
                s1 = s[i + 1] + s1;
            } else {
  
                // if length is 1, add character  
                // to end
                s1 += s[i];
            }
        }
    }
  
    return s1;
}
  
// driver program 
int main()
{
    string s = "eekgs";
    cout << decodeMedianString(s);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program to decode a median
// string to the original string
  
public class GFG {
      
    // function to calculate the 
    // median back string
    static String decodeMedianString(String s)
    {
          
        // length of string
        int l = s.length();
      
        // initialize a blank string
        String s1 = "";
      
        // Flag to check if length is
        // even or odd
        boolean isEven = (l % 2 == 0) ?
                          true : false;
      
        // traverse from first to last
        for (int i = 0; i < l; i += 2)
        {
      
            // if len is even then add
            // first character to 
            // beginning of new string 
            // and second character to
            // end
            if (isEven) { 
                s1 = s.charAt(i) + s1;
                s1 += s.charAt(i+1);
            
            else {
      
                // if current length is
                // odd and is greater 
                // than 1
                if (l - i > 1) {
      
                    // add first character
                    // to end and second
                    // character to
                    // beginning
                    s1 += s.charAt(i);
                    s1 = s.charAt(i+1) + s1;
                
                else {
      
                    // if length is 1, 
                    // add character 
                    // to end
                    s1 += s.charAt(i);
                }
            }
        }
      
        return s1;
    }
      
    // Driver code
    public static void main(String args[])
    {
        String s = "eekgs";
          
        System.out.println(
                    decodeMedianString(s));
    }
}
  
// This code is contributed by Sam007.

chevron_right


Python3

# Python3 program to decode a median
# string to the original string

# function to calculate the median
# back string
def decodeMedianString(s):

# length of string
l = len(s)

# initialize a blank string
s1 = “”

# Flag to check if length is
# even or odd
if(l % 2 == 0):
isEven = True
else:
isEven = False

# traverse from first to last
for i in range(0, l, 2):

# if len is even then add first
# character to beginning of new
# string and second character to end
if (isEven):
s1 = s[i] + s1
s1 += s[i + 1]
else :

# if current length is odd and
# is greater than 1
if (l – i > 1):

# add first character to end and
# second character to beginning
s1 += s[i]
s1 = s[i + 1] + s1
else:

# if length is 1, add character
# to end
s1 += s[i]

return s1

# Driver Code
if __name__ == ‘__main__’:
s = “eekgs”
print(decodeMedianString(s))

# This code is contributed by
# Sanjit_Prasad

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to decode a median
// string to the original string
using System;
  
class GFG {
      
    // function to calculate the 
    // median back string
    static string decodeMedianString(string s)
    {
          
        // length of string
        int l = s.Length;
      
        // initialize a blank string
        string s1 = "";
      
        // Flag to check if length is
        // even or odd
        bool isEven = (l % 2 == 0) ?
                         true : false;
      
        // traverse from first to last
        for (int i = 0; i < l; i += 2)
        {
      
            // if len is even then add
            // first character to 
            // beginning of new string 
            // and second character to
            // end
            if (isEven) { 
                s1 = s[i] + s1;
                s1 += s[i + 1];
            
            else {
      
                // if current length is
                // odd and is greater 
                // than 1
                if (l - i > 1) {
      
                    // add first character
                    // to end and second
                    // character to
                    // beginning
                    s1 += s[i];
                    s1 = s[i + 1] + s1;
                
                else {
      
                    // if length is 1, 
                    // add character 
                    // to end
                    s1 += s[i];
                }
            }
        }
      
        return s1;
    }
  
    // Driver code
    public static void Main ()
    {
        string s = "eekgs";
        Console.WriteLine(
               decodeMedianString(s));
    }
}
  
// This code is contributed by Sam007.

chevron_right



Output:

geeks

Time complexity: O(n)

This article is contributed by Striver. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Sam007, Sanjit_Prasad



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.