Find k-th character of decrypted string - Set – 2 in Python

Sometimes we need to decode an encoded string where repetitions of substrings are represented as substring followed by count of substrings. For example, if the string is "pq2rs2" and we want the 5th character, the output will be 'r', because the decrypted string is "pqpqrsrs" and the 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 't', as the decoded string is "pqpqpqpqrrtststs".

Algorithm

To solve this, we will follow these steps ?

  • Initialize an empty decoded string

  • Set occurrence = 0, i = 0

  • While i < size of string, do:

    • Extract substring (alphabetic characters)

    • Extract occurrence count (numeric characters)

    • Repeat the substring by occurrence count

  • Return the k-th character from decoded string

Example

Let us see the following implementation to get better understanding ?

def find_kth_char(string, k):
    decoded = ""
    occurrence = 0
    i = 0
    
    while i < len(string):
        temp = ""
        occurrence = 0
        
        # Extract alphabetic substring
        while (i < len(string) and ord(string[i]) >= ord('a') and ord(string[i]) <= ord('z')):
            temp += string[i]
            i += 1
            
        # Extract numeric count
        while (i < len(string) and ord(string[i]) >= ord('0') and ord(string[i]) <= ord('9')):
            occurrence = occurrence * 10 + ord(string[i]) - ord('0')
            i += 1
            
        # Repeat substring by occurrence count
        for j in range(1, occurrence + 1, 1):
            decoded += temp
            
        # If no number found, add substring once
        if occurrence == 0:
            decoded += temp
            
    return decoded[k - 1]

# Test the function
string = "pq4r2ts3"
k = 11
result = find_kth_char(string, k)
print(f"The {k}th character is: '{result}'")

# Let's also see the full decoded string
decoded_full = ""
i = 0
while i < len(string):
    temp = ""
    occurrence = 0
    
    while (i < len(string) and ord(string[i]) >= ord('a') and ord(string[i]) <= ord('z')):
        temp += string[i]
        i += 1
        
    while (i < len(string) and ord(string[i]) >= ord('0') and ord(string[i]) <= ord('9')):
        occurrence = occurrence * 10 + ord(string[i]) - ord('0')
        i += 1
        
    for j in range(1, occurrence + 1, 1):
        decoded_full += temp
        
    if occurrence == 0:
        decoded_full += temp

print(f"Decoded string: '{decoded_full}'")
print(f"Length: {len(decoded_full)}")

The output of the above code is ?

The 11th character is: 't'
Decoded string: 'pqpqpqpqrrtststs'
Length: 16

How It Works

The algorithm works by parsing the encoded string character by character:

  1. Extract substring: Read alphabetic characters to form the substring

  2. Extract count: Read numeric characters to get repetition count

  3. Repeat: Add the substring to decoded string the specified number of times

  4. Handle edge case: If no number follows a substring, add it once

For "pq4r2ts3":

  • "pq" repeated 4 times ? "pqpqpqpq"

  • "r" repeated 2 times ? "rr"

  • "ts" repeated 3 times ? "tststs"

Final decoded string: "pqpqpqpqrrtststs"

Conclusion

This solution efficiently decodes the encoded string by parsing substrings and their repetition counts. The algorithm handles multi-digit counts and returns the k-th character from the decoded string.

Updated on: 2026-03-25T09:42:02+05:30

555 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements