Find K’th Character of Decrypted String

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.

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
CodingNinjas
author
2y
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 st...read more
CodingNinjas
author
2y
Without Decryption

We will just iterate through Encrypted String ‘S’ and will keep a track on length passed in Decrypted String.

  • We will find the length of the substring (‘substringLength’) by traversin...read more
Charan Singh
1y
function findKthElement(encryptedStr , k){ let decryptedStr = '' let i = 0 //Decrypting the String while(i < encryptedStr.length){ let currentChar = '' let count= '' while(i < encryptedStr.length && i...read more
Add answer anonymously...
Deloitte Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter