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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Decode an Encoded Base 64 String to ASCII String
- Decode a string recursively encoded as count followed by substring
- Decode the string encoded with the given algorithm
- Restore original String from given Encrypted String by the given operations
- Count of ordered triplets (R, G, B) in a given original string
- Count of Distinct strings possible by inserting K characters in the original string
- Lexicographically smallest permutation with no digits at Original Index
- Median in a stream of integers (running integers)
- Median of two sorted arrays of different sizes
- Randomized Algorithms | Set 3 (1/2 Approximate Median)
- Efficiently design Insert, Delete and Median queries on a set
- Find median of BST in O(n) time and O(1) space
- Mean and Median of a matrix
- Median after K additional integers
- Minimum number of elements to add to make median equals x
- Median and Mode using Counting Sort
- Median of two sorted arrays with different sizes in O(log(min(n, m)))
- Find the median array for Binary tree
- Median of two sorted arrays of different sizes | Set 1 (Linear)
- Finding Median of unsorted Array in linear time using C++ STL