Find K’th Character of Decrypted String

Moderate
0/80
Average time to solve is 33m
profile
Contributed by
95 upvotes
Asked in companies
SamsungSiemensSwiggy

Problem statement

You have been given an Encrypted String where repetitions of substrings are represented as substring followed by the count of substrings.

Example: String "aabbbcdcdcd" will be encrypted as "a2b3cd3".

You need to find the 'K'th character of Decrypted String. Decrypted String would have 1-based indexing.

Note :

Input string will always be lowercase characters without any spaces.

If the count of a substring is 1 then also it will be followed by Integer '1'.
Example: "aabcdee" will be Encrypted as "a2bcd1e2"
This means it's guaranteed that each substring is followed by some Integer.

Also, the 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 the frequency of substring.

The frequency of a repeated substring can also be in parts.
Example: "aaaabbbb" can also have "a2a2b3b1" as Encrypted String.
Detailed explanation ( Input/output format, Notes, Images )

Input format :

The first line of each test case contains an Encrypted String 'S'. 

The second line contains the integer value 'K'.

Output format :

For each test case print, the 'K'th character of Decrypted String obtained from Encrypted String 'S'.

Note :

You are not required to print the output explicitly, it has already been taken care of. Just implement the function.

Constraints :

2 <= N <= 10^5
1 <= K <= M
1 <= M <= 10^18

Where 'N' is the length of String 'S' and 'M' is the length of the Decrypted String of 'S'. 
All the characters of String 'S' are lowercase English letters.

Time Limit: 1sec

Sample Input 1 :

a2b3cd3
8

Sample Output 1 :

c

 Explanation to Sample Input 1 :

S = "a2b3cd3"
Decrypted String of S = "aabbbcdcdcd"
According to 1-based indexing for S, the 8th character is 'c'.

Sample Input 2 :

ab12c3
20

Sample Output 2 :

b

 Explanation to Sample Input 2 :

S = "ab12c3"
Decrypted String of S = "ababababababababababababccc"
So 20th character is 'b'.
Hint

Try to Decrypt the String by traversing the Encrypted String and then return the K’th character.

Approaches (2)
Decryption

We will just iterate through Encrypted String ‘S’ and will create a Decrypted String. Then we can print the K’th character of Decrypted String.

  • We will find the substring by traversing the string until no digit is found.
  • Then find the frequency of the preceding substring by traversing the string until no lowercase alphabet is found. We can use this relation to create the Integer frequency from the string:
    • freqOfSubstring = freqOfSubstring * 10 + (S[j] - '0');
  • We will append the substring ‘freqOfSubstring’ times in Decrypted String.
  • We will keep repeating the above process until the whole string ‘S’ is traversed.
  • Finally, we have our decrypted string, print the (K-1)’th index of Decrypted String.
Time Complexity

O(M), where ‘M' is the length of the Decrypted String of Encrypted String 'S'.

 

As we are creating Decrypted String, we will append the number of characters in the Decrypted String.

Space Complexity

O(M), where ‘M'  is the length of the Decrypted String of Encrypted String 'S'.

 

As we are creating the Decrypted String.

Code Solution
(100% EXP penalty)
Find K’th Character of Decrypted String
Full screen
Console