You have been given a string 'STR' of words. You need to replace all the spaces between words with “@40”.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single string 'STR' consisting of one or more words.
Output Format:
For each test case, return the modified string after replacing all the spaces between words with “@40”.
Print the output of each test case in a separate line.
Note:
You don’t need to print anything, It has already been taken care of. Just implement the given function.
1 <= T <= 50
0 <= |STR| <= 100
Where ‘|STR|’ is the length of a particular string including spaces.
Time limit: 1 sec
2
Coding Ninjas Is A Coding Platform
Hello World
Coding@40Ninjas@40Is@40A@40Coding@40Platform
Hello@40World
In test case 1, After replacing the spaces with “@40” string is:
Coding@40Ninjas@40Is@40A@40Coding@40Platform
In test case 2, After replacing the spaces with “@40” string is:
Hello@40World
3
Hello
I love coding
Coding Ninjas India
Hello
I@40love@40coding
Coding@40Ninjas@40India
In test case 1, After replacing the spaces with “@40” string is:
Hello
In test case 2, After replacing the spaces with “@40” string is:
I@40love@40coding
In test case 3, After replacing the spaces with “@40” string is:
Coding@40Ninjas@40India
Think about using another string
The basic idea is to use a separate string to store the resultant string. Copy characters from the original string to the resultant string one by one, and whenever there comes a space, simply add “@40” in the resultant string in place of the space.
The steps are as follows:
O(N), Where ‘N’ is the size of the string of words.
Since we are traversing through the complete string of words only once, the time complexity is O(N).
O(N), Where ‘N’ is the size of the string of words.
Since we are using an auxiliary string to store the resultant string of words, the space complexity is O(N).