
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- Find k-th character of decrypted string - Set β 2 in C++
- Find kβth character of decrypted string in C++
- Check whether K-th bit is set or nots in Python
- Python program for removing n-th character from a string?
- Python β Sort String list by K character frequency
- Python β Insert character in each duplicate string after every K elements
- Program to find k-th largest XOR coordinate value in Python
- Position of the K-th set bit in a number in C++
- Find all palindromic sub-strings of a given string - Set 2 in Python
- Program to find the K-th last node of a linked list in Python
- Find K-th Smallest Pair Distance in C++
- Program to find out the k-th largest product of elements of two arrays in Python
- Python program to find k'th smallest element in a 2D array
- Find value of k-th bit in binary representation in C++
- Java program for removing n-th character from a string
