

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find k-th character of decrypted string - Set – 2 in Python
Suppose we have one encoded string where repetitions of substrings are represented as substring followed by count of substrings. As an example, if the string is "pq2rs2" and k=5, so output will be 'r', this is because the decrypted string is "pqpqrsrs" and 5th character is 'r'. We have to keep in mind that the frequency of encrypted substring can be of more than one digit.
So, if the input is like string = "pq4r2ts3" and k = 11, then the output will be i, as the string is pqpqpqpqrrtststs
To solve this, we will follow these steps −
encoded := blank string
occurrence := 0, i := 0
while i < size of str, do
temp := blank string
occurrence := 0
while i < size of str and str[i] is an alphabet, do
temp := temp + str[i]
i := i + 1
while i < size of str and str[i] is a digit, do
occurrence := occurrence * 10 + ASCII of (str[i]) - ASCII of ('0')
i := i + 1
for j in range 1 to occurrence + 1, increase by 1, do
encoded := encoded + temp
if occurrence is same as 0, then
encoded := encoded + temp
return encoded[k - 1]
Example
Let us see the following implementation to get better understanding −
def find_kth_char(str, k): encoded = "" occurrence = 0 i = 0 while i < len(str): temp = "" occurrence = 0 while (i < len(str) and ord(str[i]) >= ord('a') and ord(str[i]) <= ord('z')): temp += str[i] i += 1 while (i < len(str) and ord(str[i]) >= ord('1') and ord(str[i]) <= ord('9')): occurrence = occurrence * 10 + ord(str[i]) - ord('0') i += 1 for j in range(1, occurrence + 1, 1): encoded += temp if occurrence == 0: encoded += temp return encoded[k - 1] str = "pq4r2ts3" k = 11 print(find_kth_char(str, k))
Input
"pq4r2ts3", 11
Output
t
- Related Questions & Answers
- Find k-th character of decrypted string - Set – 2 in C++
- Find k’th character of decrypted string in C++
- Find all palindromic sub-strings of a given string - Set 2 in Python
- Check whether K-th bit is set or nots in Python
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- Python program for removing n-th character from a string?
- Program to find k-th largest XOR coordinate value in Python
- Find K-th Smallest Pair Distance in C++
- Python – Sort String list by K character frequency
- Position of the K-th set bit in a number in C++
- Find value of k-th bit in binary representation in C++
- Program to find the K-th last node of a linked list in Python
- Program to find out the k-th largest product of elements of two arrays in Python
- Find sum of Series with n-th term as n^2 - (n-1)^2 in C++
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2