Kth character after replacing each character of String by its frequency exactly X times
Given a string S consisting of N digits from [1, 9] and a positive integer K and X. Every day each character of the string is replaced by its frequency and value. The task is to find the Kth character of the string after X days.
Examples:
Input: S = “1214”, K = 10, X = 3
Output: 4
Explanation:
1st day = “12214444”
2nd day = “1222214444444444444444”
3rd day = “122222222444444444444444444444444444444444444444444444444”
So, 10th character after 3rd day is 4.
Input: S =”123″, K = 6, X = 2
Output: 3
Naive Approach: The simplest approach to solve the problem is to create the string by appending the digits in the string digitX times and finding the Kth character of the string.
C++
#include <bits/stdc++.h>
using namespace std;
char FindKthChar(string str, long long K, int X)
{
string s = str;
for ( int i = 0; i < X; i++) {
string res = "" ;
for ( int j = 0; j < s.size(); j++) {
int count = s[j] - '0' ;
while (count--) {
res += s[j];
}
}
s = res;
}
return s[K - 1];
}
int main()
{
string str = "1214" ;
long long K = 10;
int X = 3;
char ans = FindKthChar(str, K, X);
cout << ans << "\n" ;
return 0;
}
|
Java
import java.util.*;
class Gfg {
static char findKthChar(String str, long K, int X)
{
String s = str;
for ( int i = 0 ; i < X; i++) {
String res = "" ;
for ( int j = 0 ; j < s.length(); j++) {
int count = s.charAt(j) - '0' ;
while (count-- > 0 ) {
res += s.charAt(j);
}
}
s = res;
}
return s.charAt(( int )(K - 1 ));
}
public static void main(String[] args)
{
String str = "1214" ;
long K = 10 ;
int X = 3 ;
char ans = findKthChar(str, K, X);
System.out.println(ans);
}
}
|
Python3
def FindKthChar( str , K, X):
s = str
for i in range (X):
res = ""
for j in range ( len (s)):
count = int (s[j])
res + = s[j] * count
s = res
return s[K - 1 ]
str = "1214"
K = 10
X = 3
ans = FindKthChar( str , K, X)
print (ans)
|
C#
using System;
class Gfg
{
static char findKthChar( string str, long K, int X)
{
string s = str;
for ( int i = 0; i < X; i++) {
string res = "" ;
for ( int j = 0; j < s.Length; j++) {
int count = s[j] - '0' ;
while (count-- > 0) {
res += s[j];
}
}
s = res;
}
return s[( int )(K - 1)];
}
public static void Main( string [] args)
{
string str = "1214" ;
long K = 10;
int X = 3;
char ans = findKthChar(str, K, X);
Console.WriteLine(ans);
}
}
|
Javascript
function FindKthChar(str, K, X){
let s = str;
for (let i = 0; i<X; i++){
let res = "" ;
for (let j = 0; j<s.length; j++){
let count = s[j] - '0' ;
while (count--){
res += s[j];
}
}
s = res;
}
return s[K-1];
}
let str = "1214" ;
let K = 10;
let X = 3;
let ans = FindKthChar(str, K, X);
console.log(ans);
|
Time Complexity: O(?digit[i]X) for i in range [0, N-1]
Auxiliary Space: O(?digit[i]X) for i in range [0, N-1]
Efficient Approach: The above approach can be optimized further by instead of creating a string add digitX to the sum and check if sum > K. Follow the steps below to solve the problem:
- Initialize a variable, say sum that stores the sum of the character of the string after X days.
- Initialize a variable, say ans that stores the Kth characters after X days.
- Iterate in the range [0, N-1] and perform the following steps:
- Initialize a variable say range as (S[i] – ‘0’)X and add it to the variable sum.
- If sum>=K, return S, modify ans as S[i], and terminate the loop.
- Print the value of ans as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
char FindKthChar(string str, long long K, int X)
{
char ans;
int sum = 0;
for ( int i = 0; i < str.length(); i++) {
int digit = str[i] - '0' ;
int range = pow (digit, X);
sum += range;
if (K <= sum) {
ans = str[i];
break ;
}
}
return ans;
}
int main()
{
string str = "123" ;
long long K = 9;
int X = 3;
char ans = FindKthChar(str, K, X);
cout << ans << "\n" ;
return 0;
}
|
Java
class GFG{
static char FindKthChar(String str, int K, int X)
{
char ans = ' ' ;
int sum = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
int digit = ( int )str.charAt(i) - 48 ;
int range = ( int )Math.pow(digit, X);
sum += range;
if (K <= sum)
{
ans = str.charAt(i);
break ;
}
}
return ans;
}
public static void main(String[] args)
{
String str = "123" ;
int K = 9 ;
int X = 3 ;
char ans = FindKthChar(str, K, X);
System.out.println(ans);
}
}
|
Python3
import math
def FindKthChar( Str , K, X):
ans = ' '
Sum = 0
for i in range ( len ( Str )):
digit = ord ( Str [i]) - 48
Range = int (math. pow (digit, X))
Sum + = Range
if (K < = Sum ):
ans = Str [i]
break
return ans
Str = "123"
K = 9
X = 3
ans = FindKthChar( Str , K, X)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static char FindKthChar( string str, int K, int X)
{
char ans = ' ' ;
int sum = 0;
for ( int i = 0; i < str.Length; i++)
{
int digit = ( int )str[i] - 48;
int range = ( int )Math.Pow(digit, X);
sum += range;
if (K <= sum)
{
ans = str[i];
break ;
}
}
return ans;
}
public static void Main()
{
string str = "123" ;
int K = 9;
int X = 3;
char ans = FindKthChar(str, K, X);
Console.Write(ans);
}
}
|
Javascript
<script>
function FindKthChar( str , K , X) {
var ans = "" ;
var sum = 0;
for (i = 0; i < str.length; i++) {
var digit = parseInt( str[i]);
var range = parseInt( Math.pow(digit, X));
sum += range;
if (K <= sum) {
ans = str[i];
break ;
}
}
return ans;
}
var str = "123" ;
var K = 9;
var X = 3;
var ans = FindKthChar(str, K, X);
document.write(ans);
</script>
|
Time Complexity: O(N), since there is only one loop to carry out the operations the overall complexity turns out to be O(N)
Auxiliary Space: O(1), since there is no extra array or data structure used, it takes constant space.
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...