Given a string str consisting of characters and numbers and an integer k, the task is to decrypt the string and the return the kth character in the decrypted string.
In order to decrypt the string, traverse the string character by character and if the current character is an alphabet then append it to the resultant string else if it is a numeric digit then parse the number and repeat the resultant string this parsed number of times and continue with the original string. For example, str = “ab2c3” will be decrypted as “ababcababcababc”.
Input: str = “ab2c3”, k = 5
Decrypted string will be “ababcababcababc” and ‘c’ is the fifth character.
Input: str = “x2y3”, k = 3
- Initialize starting index i = 0 and total_len = 0.
- Loop while i is less then the length of the input string and check if current character is an alphabet or not. If yes then increment total_len by 1 and check if total length is less then or equal to k if yes then return the string else increment i.
- Initialize n = 0 again loop while i is less then length of input string and i is not an alphabet and parse the number and increment i and find the next_total_len = total_len * n.
- If k < total_len then get the position of the character by initializing pos = k % total_len.
- If position is not found then update position = total_len and finally return the character at kth position. If not found then return -1.
Below is the implementation of the above approach:
- Queries to find Kth greatest character in a range [L, R] from a string with updates
- Swap Kth node from beginning with Kth node from end in a Linked List
- Print Kth character in sorted concatenated substrings of a string
- Queries for rotation and Kth character of the given string in constant time
- Kth most frequent Character in a given String
- Kth character from the Nth string obtained by the given operations
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find a string such that every character is lexicographically greater than its immediate next character
- Find i'th Index character in a binary string obtained after n iterations
- Find i’th index character in a binary string obtained after n iterations | Set 2
- 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 Kth largest string from the permutations of the string with two characters
- 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
- Replace all occurrences of character X with character Y in given string
- Character replacement after removing duplicates from a string
- Minimum sum of squares of character counts in a given string after removing k characters
- Print the string after the specified character has occurred given no. of times
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.