Reverse the String

Easy
0/40
Average time to solve is 15m
profile
Contributed by
132 upvotes
Asked in companies
FacebookAckoTata Consultancy Services (TCS)

Problem statement

You are given a string 'STR'. The string contains [a-z] [A-Z] [0-9] [special characters]. You have to find the reverse of the string.

For example:

 If the given string is: STR = "abcde". You have to print the string "edcba".
follow up:
Try to solve the problem in O(1) space complexity. 
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains a single integer 'T', representing the number of test cases or queries to be run. 

Then the 'T' test cases follow.

The first and only line of each test case contains a string 'STR'.
Output Format:
For each test case, print a single line containing a single string denoting the reverse of the given string 'STR'.

The output of each test case will be printed in a separate line.

Note:

You are not required to print the expected output, it has already been taken care of. Just implement the function.
Constraints:
1 ≤ T ≤ 10
1 ≤ |STR| ≤ 10 ^ 5 

Where |STR| is the length of the string STR.

Time limit: 1 sec.
Sample Input 1:
3
abcde
coding
hello1
Sample Output 1:
edcba
gnidoc
1olleh
Explanation of the Sample Input 1:
For the first test case, STR = "abcde". We need to reverse the string, that is the first element becomes the last element and the last element becomes the first element, the second element becomes the second last element and the second last element becomes the second element and so on. So we get, "edcba".
Sample Input 2:
3
a
1det@Z
$1xYuP
Sample Output 2
a
Z@ted1
PuYx1$
Hint

Swap the first and last, second and second last characters and so on.

Approaches (1)
Optimal Solution
  • Traverse the string and swap the first character with the last character, the second character with the second last character and so on.
  • Basically, you need to swap the i-th character with the (N-i-1)-th character where N is the length of the string and 0-based indexing is considered.
Time Complexity

O(N), where ‘N’ is the length of the string.

 

Since we are iterating through the string only once, the overall time complexity is O(N).

Space Complexity

O(1).

 

In the worst case, only constant extra space is required.

Code Solution
(100% EXP penalty)
Reverse the String
Full screen
Console