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.
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.
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:
The Complexity: O(n)
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 a string recursively encoded as count followed by substring
- Decode an Encoded Base 64 String to ASCII String
- Decode a median string to the original string
- Decrypt the String according to given algorithm
- An in-place algorithm for String Transformation
- Python | Message Encode-Decode using Tkinter
- Count of occurrences of each prefix in a string using modified KMP algorithm
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Karatsuba algorithm for fast multiplication using Divide and Conquer algorithm
- Minimal moves to form a string by adding characters or appending string itself
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Find the longest sub-string which is prefix, suffix and also present inside the 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
- Longest palindromic string formed by concatenation of prefix and suffix of a 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 | Set 2
- String Range Queries to find the number of subsets equal to a given String
- String slicing in Python to check if a string can become empty by recursive deletion
- Luhn algorithm
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.