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 = 5
Output: c
Decrypted string is "aabbccc"
Input : "ab4c2ed3", k = 9
Output : c
Decrypted string is "ababababccededed"
Input: "ab4c12ed3", k = 21
Output: e
Decrypted string is "ababababccccccccccccededed"
Approach:
- The idea is simple, Initially take empty decrypted string.
- decompress the string by reading substring and it’s frequency one by one and append current substring in decrypted string by it’s frequency.
- Repeat the process till the end of string
- print the Kth character from decrypted string.
Algorithm:
- Step 1: Initialize a string with the given encoded string and the value of k.
- Step 2: Find the kth character in the encoded string.
- Step 3: Check if last character of the string is alphabet or numeric value, if it’s alphabet i.e. frequency is zero, append it to the string and return (k-1)th index.
- Step 4: Print the output.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
char encodedChar(string str, int k)
{
string expand = "" ;
string temp;
int freq = 0;
for ( int i=0; str[i]!= '\0' ; )
{
temp = "" ;
freq = 0;
while (str[i]>= 'a' && str[i]<= 'z' )
{
temp.push_back(str[i]);
i++;
}
while (str[i]>= '1' && str[i]<= '9' )
{
freq = freq*10 + str[i] - '0' ;
i++;
}
for ( int j=1; j<=freq; j++)
expand.append(temp);
}
if (freq==0)
expand.append(temp);
return expand[k-1];
}
int main()
{
string str = "ab4c12ed3" ;
int k = 21;
cout << encodedChar(str, k) << endl;
return 0;
}
|
Java
public class GFG {
static char encodedChar(String str, int k)
{
String expand = "" ;
String temp = "" ;
int freq = 0 ;
for ( int i= 0 ; i < str.length() ; )
{
temp = "" ;
freq = 0 ;
while (i < str.length() && str.charAt(i)>= 'a'
&& str.charAt(i)<= 'z' )
{
temp += str.charAt(i);
i++;
}
while (i < str.length() && str.charAt(i)>= '1'
&& str.charAt(i)<= '9' )
{
freq = freq* 10 + str.charAt(i) - '0' ;
i++;
}
for ( int j= 1 ; j<=freq; j++)
expand += temp;
}
if (freq== 0 )
expand += temp;
return expand.charAt(k- 1 );
}
public static void main(String args[])
{
String str = "ab4c12ed3" ;
int k = 21 ;
System.out.println(encodedChar(str, k));
}
}
|
Python3
def encodedChar( str , k):
expand = ""
freq = 0
i = 0
while (i < len ( str )):
temp = ""
freq = 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' )):
freq = freq * 10 + ord ( str [i]) - ord ( '0' )
i + = 1
for j in range ( 1 , freq + 1 , 1 ):
expand + = temp
if (freq = = 0 ):
expand + = temp
return expand[k - 1 ]
if __name__ = = '__main__' :
str = "ab4c12ed3"
k = 21
print (encodedChar( str , k))
|
C#
using System;
class GFG
{
static char encodedChar( string str, int k)
{
String expand = "" ;
String temp = "" ;
int freq = 0;
for ( int i = 0; i < str.Length ; )
{
temp = "" ;
freq = 0;
while (i < str.Length && str[i]>= 'a'
&& str[i]<= 'z' )
{
temp += str[i];
i++;
}
while (i < str.Length && str[i] >= '1'
&& str[i] <= '9' )
{
freq = freq * 10 + str[i] - '0' ;
i++;
}
for ( int j = 1; j <= freq; j++)
expand += temp;
}
if (freq == 0)
expand += temp;
return expand[k - 1];
}
public static void Main()
{
string str = "ab4c12ed3" ;
int k = 21;
Console.Write(encodedChar(str, k));
}
}
|
Javascript
<script>
function encodedChar(str, k)
{
let expand = "" ;
let temp = "" ;
let freq = 0;
for (let i=0; i < str.length ; )
{
temp = "" ;
freq = 0;
while (i < str.length && str[i].charCodeAt(0)>= 'a' .charCodeAt(0)
&& str[i].charCodeAt(0)<= 'z' .charCodeAt(0))
{
temp += str[i];
i++;
}
while (i < str.length && str[i].charCodeAt(0)>= '1' .charCodeAt(0)
&& str[i].charCodeAt(0)<= '9' .charCodeAt(0))
{
freq = freq*10 + str[i].charCodeAt(0) - '0' .charCodeAt(0);
i++;
}
for (let j=1; j<=freq; j++)
expand += temp;
}
if (freq==0)
expand += temp;
return expand[k-1];
}
let str = "ab4c12ed3" ;
let k = 21;
document.write(encodedChar(str, k));
</script>
|
Time Complexity: O(N) where N is length of string.
Auxiliary Space: O(N)
Exercise : The above solution builds the decoded string to find k’th character. Extend the solution to work in O(1) extra space.
Find k-th character of decrypted string | Set – 2
This article is contributed by Shashank Mishra ( Gullu ) and reviewed by team GeeksforGeeks. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.