Given an encoded string where repetitions of substrings are represented as substring followed by count of substrings. For example, if encrypted string is “ab2cd2” and k=4 , so output will be ‘b’ because decrypted string is “ababcdcd” and 4th character is ‘b’.
Note: Frequency of encrypted substring can be of more than one digit. For example, in “ab12c3”, ab is repeated 12 times. No leading 0 is present in frequency of substring.
Input: "a2b2c3", k = 5 Output: c Decrypted string is "aabbccc" Input : "ab4c2ed3", k = 9 Output : c Decrypted string is "ababababccededed" Input: "ab4c12ed3", k = 21 Output: e Decrypted string is "ababababccccccccccccededed"
The idea is simple. Initially take empty decrypted string then decompress the string by reading substring and it’s frequency one by one and append current substring in decrypted string by it’s frequency. Repeat the process till the end of string and print the K’th character from decrypted string.
Exercise : The above solution builds the decoded string to find k’th character. Extend the solution to work in O(1) extra space.
This article is contributed by Shashank Mishra ( Gullu ) and reviewed by team GeeksforGeeks. 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 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.
- Find k-th character of decrypted string | Set - 2
- Find a string such that every character is lexicographically greater than its immediate next character
- Find the character in first string that is present at minimum index in second string
- Queries to find the first non-repeating character in the sub-string of a string
- Queries to find the last non-repeating character in the sub-string of a given string
- Find the last non repeating character in string
- Find last index of a character in a string
- Given a string, find its first non-repeating character
- Find the Nth occurrence of a character in the given String
- Find the first repeated character in a string
- Find one extra character in a string
- Find repeated character present first in a string
- Program to find the kth character after decrypting a string
- Find the last remaining Character in the Binary String according to the given conditions
- Find the largest Alphabetic character present in the string
- Find the character made by adding all the characters of the given string
- Find first non-repeating character in a given string using Linked List
- Find i'th Index character in a binary string obtained after n iterations
- Queries to find total number of duplicate character in range L to R in the string S
- Efficiently find first repeated character in a string without using any additional data structure in one traversal