# Kth Character After Replacing Each Character of String by its Frequency Exactly X Times

In this problem we have given a string ‘str’, integer K, and integer X. That string ‘str’ contains only integers in the range between 1 to 9. We have to perform an operation over the string exactly X times. Operation is that every time we have to replace a character of string by itself its frequency times. Here the frequency means the number or the value of the character of the string. Our task is to return the kth character after doing a given operation exactly X times.

### Sample Examples

Input 1: str = “1231”, K = 5, X = 3

Output 1: 2


### Explanation

we have performed an operation 3 times as given.

1st time, str = 1223331 as

• For character str[0], frequency is 1 and value is 1, so 1 comes 1 time.

• For character str[1], frequency is 2 and value is 2, so 2 comes 2 times.

• Similarly for other characters.

2nd time, str = 122223333333331
3rd time, str = 1222222223333333333333333333333333331


So the Kth character of the string after exactly X times is 2. So the answer is 2.

Input 2: str = “1121”,  K = 2, X = 5

Output 2: 2


We have seen the example above for the given string, let us move to the approaches −

## Naive Approach

In this approach, we calculate new string till the X times by performing a given operation. After getting the string of exactly X times we return the Kth character of that string.

### Example

Let’s see the code for better understanding of the above approach −

#include <bits/stdc++.h>
using namespace std;

// Function to find the Kth character of the string after X times
char findKthChar(string str, long long K, int X){
string s = str; // create another string to store the give string as we need to update the string
for (int i = 0; i < X; i++) {
string temp = ""; // To store the temporary result of each time
for (int j = 0; j < s.size(); j++) {
int freq = s[j] - '0'; // getting freq of char s[j]

// adding char value its frequency times to 'temp' result.
while (freq--) {
temp += s[j];
}
}
s = temp; // update the string after.
}
return s[K - 1]; // return Kth character of X times string
}
int main(){

// Given Input
string str = "1231";
long long K =  5;
int X = 3;

// Function Call
char result = findKthChar(str, K, X);
cout << result << "\n";
return 0;
}


### Output

2


Time and Space Complexity

The time complexity depends upon the given string digits and will equal to the digits to their power x and sum of each of them.

The space complexity is exactly the same as the time complexity.

## Efficient Approach

It is an optimized version of the above approach. In which we are calculating the range of each charter for the X times instead of creating a string for every time.

Here we observed that every time the character increases by the power of time with respect to the character value.

Let’s discuss the main steps of the above approach below −

• Created kthChar variable to store the KthChar of x times string

• Created variable tot to store the count of each character occurrence after the X times

• Traverse the string using for loop and perform following steps

• −>Getting value of current character

−>Using that value and X we get the range of that current character after the X times. As we observed that every time a character is increase in the value’s power X

As pow(value, X).

−> Store that range in variable ‘tot’ to maintain the length of the string after the X times

−> Check whether the Kth character lie in the current length of the string after the X times

As (K <= tot) if yes than break the for loop and store current character into variable ‘kthChar’

• Return kthChar

### Example

#include <bits/stdc++.h>
using namespace std;

// Function to find the Kth character of the string after X times
char findKthChar(string str, long long K, int X){
char kthChar; // Variable to store the KthChar of x times string
int tot = 0; // to store the count of the each character occur after the X times

// Traverse the string 'str'
for (int i = 0; i < str.size(); i++) {
int value = str[i] - '0'; // Convert char into int to get the value

// Calculate each characters occuring range
int charRange = pow(value, X);
tot += charRange;

// If K is less than tot than kthChar is str[i]
if (K <= tot) {
kthChar = str[i];
break; // break the for loop
}
}

// Return answer, kthChar of the string after X times
return kthChar;
}
int main(){
string str = "1231"; // given string
long long K =  5; // given integer
int X = 3; // given integer

// Function Call to get the kth character after X times
char result = findKthChar(str, K, X);

// Print the result
cout << result << "\n";
return 0;
}


### Output

2


Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given length.

The space complexity of the above code is O(1), as we are not using any extra space.

## Conclusion

In this tutorial, we have implemented a program to find the Kth character after replacing each character of String by its frequency exactly X times. We have implemented two approaches, one is the Naive approach and another one is the efficient approach.

Updated on: 25-Jul-2023

43 Views