
Input: ‘N’ = 8, 'K' = ?, ‘STR’ = A12a3CbB
Output: A1?a?CbB
In this case, the numbers are ‘1’, ‘2’, and ‘3’ at ‘2nd’, ‘3rd’, and ‘5th’ positions respectively (1-based indexing). But only ‘2’ and ‘3’ are prime numbers, So after replacing them with ‘K’ the final string will be “A1?a?CbB”.
The first line will contain the integer ‘T’, the number of test cases.
Each test case consists of two lines.
The first line of input contains one integer and one character, ‘N’ and ‘K’ separated by spaces.
Followed by a line containing the string ‘STR’ of length ‘N’.
For each test case, print the correct final string after replacement.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
‘K’ is a lower-case or upper-case English letter or any special character like ‘#’, ‘?’, ‘%’, ‘@’, ‘(’ and ‘)’.
‘STR’ consists of lower-case or uppercase letters or digits.
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
Time Limit: 1 sec
The idea for this approach is to first find or hard-code all the prime numbers from ‘0’ to ‘9’ and then iterate on the given string and if found any prime numbers, replace them with the given character ‘K’.
We can store all the prime numbers into a hash-set for O(1) access.
Algorithm:
// The function will return true if the given character is a number.
Int isDigit(X)
// The function will return the correct final string after replacement
String kReplacement(N, K, STR)