Program to find the kth character after decrypting a string
Given a string str consisting of characters and numbers and an integer k, the task is to decrypt the string and the return the kth character in the decrypted string.
In order to decrypt the string, traverse the string character by character and if the current character is an alphabet then append it to the resultant string else if it is a numeric digit then parse the number and repeat the resultant string this parsed number of times and continue with the original string. For example, str = “ab2c3” will be decrypted as “ababcababcababc”.
Examples:
Input: str = “ab2c3”, k = 5
Output: c
Decrypted string will be “ababcababcababc” and ‘c’ is the fifth character.
Input: str = “x2y3”, k = 3
Output: y
Approach:
- Initialize starting index i = 0 and total_len = 0.
- Loop while i is less than the length of the input string and check if current character is an alphabet or not. If yes then increment total_len by 1 and check if total length is less than or equal to k if yes then return the string else increment i.
- Initialize n = 0 again loop while i is less than length of input string and i is not an alphabet and parse the number and increment i and find the next_total_len = total_len * n.
- If k < total_len then get the position of the character by initializing pos = k % total_len.
- If position is not found then update position = total_len and finally return the character at kth position. If not found then return -1.
Below is the implementation of the above approach:
C++
#include <cstdlib>
#include <iostream>
using namespace std;
char findKthChar(string s, int k)
{
int len = s.length();
int i = 0;
int total_len = 0;
while (i < len) {
if ( isalpha (s[i])) {
total_len++;
if (total_len == k)
return s[i];
i++;
}
else {
int n = 0;
while (i < len && ! isalpha (s[i])) {
n = n * 10 + (s[i] - '0' );
i++;
}
int next_total_len = total_len * n;
if (k <= next_total_len) {
int pos = k % total_len;
if (!pos) {
pos = total_len;
}
return findKthChar(s, pos);
}
else {
total_len = next_total_len;
}
}
}
return -1;
}
int main()
{
string s = "ab2c3" ;
int k = 5;
cout << findKthChar(s, k);
return 0;
}
|
Java
import java.util.*;
class GfG
{
static Character findKthChar(String s, int k)
{
int len = s.length();
int i = 0 ;
int total_len = 0 ;
while (i < len)
{
if (Character.isLetter(s.charAt(i)))
{
total_len++;
if (total_len == k)
return s.charAt(i);
i++;
}
else
{
int n = 0 ;
while (i < len && !Character.isLetter(s.charAt(i)))
{
n = n * 10 + (s.charAt(i) - '0' );
i++;
}
int next_total_len = total_len * n;
if (k <= next_total_len)
{
int pos = k % total_len;
if (pos == 0 )
{
pos = total_len;
}
return findKthChar(s, pos);
}
else
{
total_len = next_total_len;
}
}
}
return ' ' ;
}
public static void main(String[] args)
{
String s = "ab2c3" ;
int k = 5 ;
System.out.println(findKthChar(s, k));
}
}
|
Python3
def findKthChar(s, k):
len1 = len (s)
i = 0
total_len = 0
while (i < len1):
if (s[i].isalpha()):
total_len + = 1
if (total_len = = k):
return s[i]
i + = 1
else :
n = 0
while (i < len1 and s[i].isalpha() = = False ):
n = n * 10 + ( ord (s[i]) - ord ( '0' ))
i + = 1
next_total_len = total_len * n
if (k < = next_total_len):
pos = k % total_len
if (pos = = 0 ):
pos = total_len
return findKthChar(s, pos)
else :
total_len = next_total_len
return - 1
if __name__ = = '__main__' :
s = "ab2c3"
k = 5
print (findKthChar(s, k))
|
C#
using System;
class GFG
{
static char findKthChar(String s, int k)
{
int len = s.Length;
int i = 0;
int total_len = 0;
while (i < len)
{
if ( char .IsLetter(s[i]))
{
total_len++;
if (total_len == k)
return s[i];
i++;
}
else
{
int n = 0;
while (i < len && ! char .IsLetter(s[i]))
{
n = n * 10 + (s[i] - '0' );
i++;
}
int next_total_len = total_len * n;
if (k <= next_total_len)
{
int pos = k % total_len;
if (pos == 0)
{
pos = total_len;
}
return findKthChar(s, pos);
}
else
{
total_len = next_total_len;
}
}
}
return ' ' ;
}
public static void Main(String[] args)
{
String s = "ab2c3" ;
int k = 5;
Console.WriteLine(findKthChar(s, k));
}
}
|
Javascript
function findKthChar(s, k)
{
let len = s.length;
let i = 0;
let total_len = 0;
let isAlpha = function (ch){
return typeof ch === "string" && ch.length === 1 && (ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" );
}
while (i < len) {
if (isAlpha(s[i])) {
total_len++;
if (total_len == k)
return s[i];
i++;
}
else {
let n = 0;
while (i < len && !isAlpha(s[i])) {
n = n * 10 + (s[i] - '0' );
i++;
}
let next_total_len = total_len * n;
if (k <= next_total_len) {
let pos = k % total_len;
if (!pos) {
pos = total_len;
}
return findKthChar(s, pos);
}
else {
total_len = next_total_len;
}
}
}
return -1;
}
let s = "ab2c3" ;
let k = 5;
console.log(findKthChar(s, k));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
20 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...