Bob is a student and learned about alphabets and prime numbers today. He still sometimes gets confused between normal numbers and prime numbers. So his teacher thought of giving him a fun game that can help him to get confident in this topic.
The game was, there is string βSTRβ of length βNβ which includes alphabets and digits. There is also a special character βKβ. The game was to replace every prime number that occurred in the string βSTRβ with βKβ.
Bob has solved the task and finished the game, but he still has doubt that the final string he got after the replacement is correct. So being his friend he asked you to help him with it.
So your task is to play the same game and return the correct final string after replacement.
NOTE: 1 is neither prime nor composite.
EXAMPLE :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β.
Output format :
For each test case, print the correct final string after replacement.
Note :
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
2
3 ?
1a2
6 %
Ax1yB3
1a?
Ax1yB%
For the first test case, the numbers are at the β1stβ and β3rdβ positions. But only β2β is prime. Hence the final string after the replacement is β1a?β.
Hence, the output will be: 1a?
For the second test case, the numbers are at the β3rdβ and β6thβ positions respectively. But only β3β is prime. Hence the final string after the replacement is βAx1yB%β.
Hence, the output will be: Ax1yB%
2
7 #
AbcDFgx
10 (
0123456789
AbcDFgx
01((4(6(89
Can we store all the primes and use them in some way?.
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)
O(N), Where βNβ is the input integer.
We iterate on the given string βSTRβ of length βNβ and replace all the prime numbers with βKβ in O(1), hence the time complexity will be O(N)
O(1),
As we only used some temporary loop variables and a hash-set of size β4β, the space complexity will be O(1).