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. Examples:
Input: "a2b2c3", k = 5
Decrypted string is "aabbccc"
Input : "ab4c2ed3", k = 9
Output : c
Decrypted string is "ababababccededed"
Input: "ab4c12ed3", k = 21
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.
// C++ program to find K'th character in
// decrypted string
// Function to find K'th character in Encoded String
// expand string variable is used to
// store final string after decompressing string str
// This code is contributed by avanitrachhadiya2155
Exercise : The above solution builds the decoded string to find k’th character. Extend the solution to work in O(1) extra space. Find k-th character of decrypted string | Set – 2 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.
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.