-
Notifications
You must be signed in to change notification settings - Fork 618
/
reverse_string.java
60 lines (50 loc) · 1.74 KB
/
reverse_string.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Reverse a string using 2 pointer Approach
Problem Statement: Reverse the given string from user.
For e.g initial string: hello, reversed string: olleh.
Using two pointer approach, firstly 'h' and 'o' are swapped similarly other characters follow the same sequence.
In this way, the string is reversed without using extra space.
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class reverse_string{
// function for reversing the given string
public static void ReverseTheString(){
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
int k = input.length();
//converting it into array to access each Character of string
char[] output = input.toCharArray();
//using two pointer type approach to swap the characters of string
//one pointer points to starting and other to ending
for(int itr = 0, ptr = k-1; itr < ptr; itr++, ptr--){
char flag = output[itr];
output[itr] = output[ptr];
output[ptr] = flag;
}
System.out.print("Reversed string: ");
for(int itr = 0; itr < k; itr++){
System.out.print(output[itr]);
}
}
public static void main (String[] args) throws java.lang.Exception{
//calling for the reverse function from main
ReverseTheString();
}
}
/*
Test Cases:
1.
Input -
towerofhanoi
Output -
Reversed string: ionahforewot
2.
Input -
reversemalayalam
Output -
Reversed string: malayalamesrever
Time Complexity: O(k), for traversing the array of characters where 'k' is the size of input string
Space Complexity: O(1), no extra space is used
*/