Problem of the day
You are having a string ‘S’ containing ASCII characters.
You are required to reverse the input string.
Output the reverse of the string ‘S’.
S = “hello”
Explanation :
The reverse of the string ‘S’ is “olleh”.
The first line contains an integer 'T' which denotes the number of test cases to be run. Then the test cases follow.
The only line of each test case contains a string ‘S’.
For each test case, print a string denoting the reverse of the string ‘S’.
Print the output of each test case in a new line.
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= S.length <= 10^5
Time Limit: 1 sec
2
ninja
MoM
ajnin
MoM
For test case 1 we have,
The reverse of “ninja” is “ajnin”.
So, we output “ajnin” ( without quotes ).
For test case 2 we have,
The reverse of “MoM” is “MoM” as it is a palindrome.
So, we output “MoM”.
2
ggUM
Kzk
MUgg
kzK
Think about swapping characters to reverse the string.
Approach :
Algorithm :
O(N), where ‘N’ is the length of the string ‘S’.
We are traversing the string till ‘N/2’, so the overall time complexity is O(N).
O(1)
Constant extra space is required. Hence, the overall Space Complexity is O(1).