# Find k-th character of decrypted string | Set – 2

Given an encoded string where repetitions of substrings are represented as substring followed by count of substrings. For example, if encrypted string is “ab2cd2” and k=4, so output will be ‘b’ because decrypted string is “ababcdcd” and 4th character is ‘b’.

**Note: **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 frequency of substring.

**Examples:**

Input:"a2b2c3", k = 5Output:c Decrypted string is "aabbccc"Input:"ab4c2ed3", k = 9Output :c Decrypted string is "ababababccededed"

The solution discussed in previous post requires additional O(n) space. The following post discuss a solution that requires constant space. The stepwise algorithm is:

- Find length of current substring. Use two pointers. Fix one pointer at beginning of substring and move another pointer until a digit is not found.
- Find frequency of repetition by moving the second pointer further until an alphabet is not found.
- Find length of substring if it is repeated by multiplying frequency and its original length.
- If this length is less than k, then required character lies in substring that follows. Subtract this length from k to keep count of number of characters that are required to be covered.
- If length is less than or equal to k, then required character lies in current substring. As k is 1-indexed reduce it by 1 and then take its mod with original substring length. Required character is kth character from starting of substring which is pointed by first pointer.

Below is the implementation of the above approach:

## C++

`// C++ program to find K'th character in ` `// decrypted string ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find K'th character in ` `// Encoded String ` `char` `encodedChar(string str, ` `int` `k) ` `{ ` ` ` `int` `i, j; ` ` ` ` ` `int` `n = str.length(); ` ` ` ` ` `// To store length of substring ` ` ` `int` `len; ` ` ` ` ` `// To store length of substring when ` ` ` `// it is repeated ` ` ` `int` `num; ` ` ` ` ` `// To store frequency of substring ` ` ` `int` `freq; ` ` ` ` ` `i = 0; ` ` ` ` ` `while` `(i < n) { ` ` ` `j = i; ` ` ` `len = 0; ` ` ` `freq = 0; ` ` ` ` ` `// Find length of substring by ` ` ` `// traversing the string until ` ` ` `// no digit is found. ` ` ` `while` `(j < n && ` `isalpha` `(str[j])) { ` ` ` `j++; ` ` ` `len++; ` ` ` `} ` ` ` ` ` `// Find frequency of preceding substring. ` ` ` `while` `(j < n && ` `isdigit` `(str[j])) { ` ` ` `freq = freq * 10 + (str[j] - ` `'0'` `); ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// Find length of substring when ` ` ` `// it is repeated. ` ` ` `num = freq * len; ` ` ` ` ` `// If length of repeated substring is less than ` ` ` `// k then required character is present in next ` ` ` `// substring. Subtract length of repeated ` ` ` `// substring from k to keep account of number of ` ` ` `// characters required to be visited. ` ` ` `if` `(k > num) { ` ` ` `k -= num; ` ` ` `i = j; ` ` ` `} ` ` ` ` ` `// If length of repeated substring is ` ` ` `// more or equal to k then required ` ` ` `// character lies in current substring. ` ` ` `else` `{ ` ` ` `k--; ` ` ` `k %= len; ` ` ` `return` `str[i + k]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// This is for the case when there ` ` ` `// are no repetition in string. ` ` ` `// e.g. str="abced". ` ` ` `return` `str[k - 1]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string str = ` `"abced"` `; ` ` ` `int` `k = 4; ` ` ` ` ` `cout << encodedChar(str, k) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find K'th character in ` `// decrypted string ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find K'th character in ` `// Encoded String ` `static` `char` `encodedChar(` `char` `[] str, ` `int` `k) ` `{ ` ` ` `int` `i, j; ` ` ` ` ` `int` `n = str.length; ` ` ` ` ` `// To store length of substring ` ` ` `int` `len; ` ` ` ` ` `// To store length of substring when ` ` ` `// it is repeated ` ` ` `int` `num; ` ` ` ` ` `// To store frequency of substring ` ` ` `int` `freq; ` ` ` ` ` `i = ` `0` `; ` ` ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` `j = i; ` ` ` `len = ` `0` `; ` ` ` `freq = ` `0` `; ` ` ` ` ` `// Find length of substring by ` ` ` `// traversing the string until ` ` ` `// no digit is found. ` ` ` `while` `(j < n && Character.isAlphabetic(str[j])) ` ` ` `{ ` ` ` `j++; ` ` ` `len++; ` ` ` `} ` ` ` ` ` `// Find frequency of preceding substring. ` ` ` `while` `(j < n && Character.isDigit(str[j])) ` ` ` `{ ` ` ` `freq = freq * ` `10` `+ (str[j] - ` `'0'` `); ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// Find length of substring when ` ` ` `// it is repeated. ` ` ` `num = freq * len; ` ` ` ` ` `// If length of repeated substring is less than ` ` ` `// k then required character is present in next ` ` ` `// substring. Subtract length of repeated ` ` ` `// substring from k to keep account of number of ` ` ` `// characters required to be visited. ` ` ` `if` `(k > num) ` ` ` `{ ` ` ` `k -= num; ` ` ` `i = j; ` ` ` `} ` ` ` ` ` `// If length of repeated substring is ` ` ` `// more or equal to k then required ` ` ` `// character lies in current substring. ` ` ` `else` ` ` `{ ` ` ` `k--; ` ` ` `k %= len; ` ` ` `return` `str[i + k]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// This is for the case when there ` ` ` `// are no repetition in string. ` ` ` `// e.g. str="abced". ` ` ` `return` `str[k - ` `1` `]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str = ` `"abced"` `; ` ` ` `int` `k = ` `4` `; ` ` ` ` ` `System.out.println(encodedChar(str.toCharArray(), k)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find K'th ` `# character in decrypted string ` ` ` `# Function to find K'th character ` `# in Encoded String ` `def` `encodedChar(string, k): ` ` ` ` ` `n ` `=` `len` `(string) ` ` ` ` ` `i ` `=` `0` ` ` `while` `i < n: ` ` ` `j ` `=` `i ` ` ` `length ` `=` `0` ` ` `freq ` `=` `0` ` ` ` ` `# Find length of substring by ` ` ` `# traversing the string until ` ` ` `# no digit is found. ` ` ` `while` `j < n ` `and` `string[j].isalpha(): ` ` ` `j ` `+` `=` `1` ` ` `length ` `+` `=` `1` ` ` ` ` `# Find frequency of preceding substring. ` ` ` `while` `j < n ` `and` `string[j].isdigit(): ` ` ` `freq ` `=` `freq ` `*` `10` `+` `int` `(string[j]) ` ` ` `j ` `+` `=` `1` ` ` ` ` `# Find the length of the substring ` ` ` `# when it is repeated. ` ` ` `num ` `=` `freq ` `*` `length ` ` ` ` ` `# If the length of the repeated substring ` ` ` `# is less than k then required character ` ` ` `# is present in next substring. Subtract ` ` ` `# the length of repeated substring from ` ` ` `# k to keep account of the number ` ` ` `# of characters required to be visited. ` ` ` `if` `k > num: ` ` ` `k ` `-` `=` `num ` ` ` `i ` `=` `j ` ` ` ` ` `# If length of repeated substring is ` ` ` `# more or equal to k then required ` ` ` `# character lies in current substring. ` ` ` `else` `: ` ` ` `k ` `-` `=` `1` ` ` `k ` `%` `=` `length ` ` ` `return` `string[i ` `+` `k] ` ` ` ` ` `# This is for the case when there are no ` ` ` `# repetition in string. e.g. str="abced". ` ` ` `return` `string[k ` `-` `1` `] ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `string ` `=` `"abced"` ` ` `k ` `=` `4` ` ` ` ` `print` `(encodedChar(string, k)) ` ` ` `# This code is contributed ` `# by Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find K'th character in ` `// decrypted string ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find K'th character in ` `// Encoded String ` `static` `char` `encodedChar(` `char` `[] str, ` `int` `k) ` `{ ` ` ` `int` `i, j; ` ` ` ` ` `int` `n = str.Length; ` ` ` ` ` `// To store length of substring ` ` ` `int` `len; ` ` ` ` ` `// To store length of substring when ` ` ` `// it is repeated ` ` ` `int` `num; ` ` ` ` ` `// To store frequency of substring ` ` ` `int` `freq; ` ` ` ` ` `i = 0; ` ` ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` `j = i; ` ` ` `len = 0; ` ` ` `freq = 0; ` ` ` ` ` `// Find length of substring by ` ` ` `// traversing the string until ` ` ` `// no digit is found. ` ` ` `while` `(j < n && ` `char` `.IsLetter(str[j])) ` ` ` `{ ` ` ` `j++; ` ` ` `len++; ` ` ` `} ` ` ` ` ` `// Find frequency of preceding substring. ` ` ` `while` `(j < n && ` `char` `.IsDigit(str[j])) ` ` ` `{ ` ` ` `freq = freq * 10 + (str[j] - ` `'0'` `); ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// Find length of substring when ` ` ` `// it is repeated. ` ` ` `num = freq * len; ` ` ` ` ` `// If length of repeated substring is less than ` ` ` `// k then required character is present in next ` ` ` `// substring. Subtract length of repeated ` ` ` `// substring from k to keep account of number of ` ` ` `// characters required to be visited. ` ` ` `if` `(k > num) ` ` ` `{ ` ` ` `k -= num; ` ` ` `i = j; ` ` ` `} ` ` ` ` ` `// If length of repeated substring is ` ` ` `// more or equal to k then required ` ` ` `// character lies in current substring. ` ` ` `else` ` ` `{ ` ` ` `k--; ` ` ` `k %= len; ` ` ` `return` `str[i + k]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// This is for the case when there ` ` ` `// are no repetition in string. ` ` ` `// e.g. str="abced". ` ` ` `return` `str[k - 1]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String str = ` `"abced"` `; ` ` ` `int` `k = 4; ` ` ` ` ` `Console.WriteLine(encodedChar(str.ToCharArray(), k)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

e

**Time Complexity:** O(n)

**Auxiliary Space:** O(1)

## Recommended Posts:

- Find k'th character of decrypted string | Set 1
- Find a string such that every character is lexicographically greater than its immediate next character
- Find the character in first string that is present at minimum index in second string
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Find the last non repeating character in string
- Find one extra character in a string
- Find last index of a character in a string
- Given a string, find its first non-repeating character
- Find the first repeated character in a string
- Program to find the kth character after decrypting a string
- Find repeated character present first in a string
- Find the character made by adding all the characters of the given string
- Find i'th Index character in a binary string obtained after n iterations
- Efficiently find first repeated character in a string without using any additional data structure in one traversal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.