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.
- Decode an Encoded Base 64 String to ASCII String
- Decode a string recursively encoded as count followed by substring
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Minimal moves to form a string by adding characters or appending string itself
- String slicing in Python to check if a string can become empty by recursive deletion
- Create a new string by alternately combining the characters of two halves of the string in reverse
- String Range Queries to find the number of subsets equal to a given String
- Minimum number of given operations required to convert a string to another string
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find the character in first string that is present at minimum index in second string
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Find length of longest subsequence of one string which is substring of another string