Given a string S containing letter and digit and an integer K where,
The new string S’ is formed from old string S by following steps:
1. If the character read is a letter, that letter is added at end of S’.
2. If the character read is a digit, then entire string S’ repeatedly written d-1 more times in total.
Note: The new string is guaranteed to have less than 2^63 letters.
Input: S = “geeks2for2”, K = 15
Explanation: The new string S’ = “geeksgeeksforgeeksgeeksfor”. The 15th letter is “e”.
Input: S = “a2345”, K = 100
Explanation: The new string S’=”a” repeated 120 times. The 100th letter is “a”.
Let us take a new string like S’ = “geeksgeeksgeeksgeeksgeeks” and an index K = 22, then the answer at K = 22 is the same if K = 2.
In general, when a string is equal to some word with size length repeated some number of times (such as geeks with size = 5 repeated 5 times), then the answer will be same for the index K as it is for the index K % size.
Using this insight and working backwards, we keep track of the size of the new string S’. Whenever the string S’ would equal some word repeated d times, we can reduce K to K % (lengthof(word)).
We first find the length of the new string S’. After, this we’ll work backwards, keeping track of size: the length of the new string after parsing symbols S, S, …, S[i].
If we see a digit S[i], it means the size of the new string after parsing S, S, …, S[i-1] will be (size / toInteger(S[i])). Otherwise, it will be size – 1.
Below is the implementation of above approach:
Time Complexity: O(N), where N is the length of S.
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 the given decoded string is divisible by 6
- Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word
- Get the first letter of each word in a string using regex in Java
- Print Kth character in sorted concatenated substrings of a string
- String containing first letter of every word in a given string with spaces
- Count of all unique substrings with non-repeating characters
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Strings formed from given characters without any consecutive repeating characters
- Check if the string contains consecutive letters and each letter occurs exactly once
- Number of positions where a letter can be inserted such that a string becomes palindrome
- Program to accept String starting with Capital letter
- First uppercase letter in a string (Iterative and Recursive)
- Move all occurence of letter 'x' from the string s to the end using Recursion
- Count of Substrings that can be formed without using the given list of Characters
- Count of substrings formed using a given set of characters only
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Find letter's position in Alphabet using Bit operation
- Minimum letters to be removed to make all occurrences of a given letter continuous
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
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.