K-Replacement

Easy
0/40
Average time to solve is 5m
profile
Contributed by
3 upvotes
Asked in company
Zivost Technologies

Problem statement

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”.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
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.
Constraints :
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
Sample Input 1 :
2
3 ?
1a2
6 %
Ax1yB3
Sample Output 1 :
1a?
Ax1yB%
Explanation Of Sample Input 1 :
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%
Sample Input 2 :
2
7 #
AbcDFgx
10 (
0123456789
Sample Output 2 :
AbcDFgx
01((4(6(89
Hint

Can we store all the primes and use them in some way?.

Approaches (1)
String Manipulation

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)

  • If X lies in the range β€˜0’ to β€˜9’.
    • Return the value of β€˜X’ in the decimal system.
  • Return β€˜-1’.

 

// The function will return the correct final string after replacement

String kReplacement(N, K, STR) 

  • Create a hash-set β€˜PRIMES’ and insert β€˜2’, β€˜3’, β€˜5’, and β€˜7’ into the map.
  • Run a for loop from β€˜0’ to β€˜N-1’ with a variable named β€˜i’.
    • Initialize a variable named β€˜DIGIT’ with the value of β€˜isDigit(STR[i])’.
    • If β€˜DIGIT != -1’ and β€˜DIGIT’ is present in β€˜PRIMES’.
      • Assign β€˜STR[i]’ value of β€˜K’.
  • Return β€˜STR’.
Time Complexity

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)

Space Complexity

O(1),

 

As we only used some temporary loop variables and a hash-set of size β€˜4’, the space complexity will be O(1).

Code Solution
(100% EXP penalty)
K-Replacement
Full screen
Console