Given a string str of length N, the task is to decrypt it using a given set of decryption rules and print the decrypted string.
The decryption rules are as follows:
- Start with the middle character of the string str and print it.
- Repetitively traverse the right substring and print its middle character.
- Repeat the same procedure for the left substring too.
Input: N = 4, str = "abcd" Output: bcda Explanation: abcd ------ b / \ a cd ------ c / \ a d ----- d / a --------------- a Hence, the final string is "bcda". Input: N = 6, str = "gyuitp" Output: utpigy Explanation: gyuitp ------- u / \ gy itp ------- t / /\ gy i p ------ p / / gy i ----------- i / gy --------------- g \ y -------------- y Hence, the final string is "utpigy".
The main idea is to use recursion. Keep on dividing the whole string into left and right substrings and print the middle element of every such substring, until the string is left with a single character and can not be further divided.
Detailed steps for this approach are as follows:
- Initialize start = 0, end = N -1, denoting the first and last character of the string.
- Print the character at the middle of the string, that is mid = (start + end) / 2.
- Recursively traverse its right substring (start = mid +1, end) followed by its left substring (start, mid – 1).
- Repeat the above steps for each substring traversed. Continue until the entire string is traversed and print the given string.
Below is the implementation of the above approach:
Time complexity: O(N)
Auxiliary Space: O(1)
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.
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Traverse Linked List from middle to left-right order using recursion
- Decode a string recursively encoded as count followed by substring
- Rearrange a string in the form of integer sum followed by the minimized character
- Maximize count of 0s in left and 1s in right substring by splitting given Binary string
- Check If every group of a's is followed by a group of b's of same length
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Print the middle character of a string
- Check if left and right shift of any string results into given string
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Minimum K such that every substring of length atleast K contains a character c
- Rearrange a string in sorted order followed by the integer sum
- Check if a string can be split into substrings starting with N followed by N characters
- 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
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.