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.
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
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 firstname.lastname@example.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Decode an Encoded Base 64 String to ASCII String
- Decode the string encoded with the given algorithm
- Decode a string recursively encoded as count followed by substring
- Count of ordered triplets (R, G, B) in a given original string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Transform string str1 into str2 by taking characters from string str3
- String slicing in Python to check if a string can become empty by recursive deletion
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- String Range Queries to find the number of subsets equal to a given String
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Minimal moves to form a string by adding characters or appending string itself
- Minimum number of given operations required to convert a string to another string
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Find the count of palindromic sub-string of a string in its sorted form
- Check if a string can be formed from another string by at most X circular clockwise shifts
- String obtained by reversing and complementing a Binary string K times
- Check if a string can be converted to another string by replacing vowels and consonants