File tree 1 file changed +12
-14
lines changed
1 file changed +12
-14
lines changed Original file line number Diff line number Diff line change @@ -16,19 +16,17 @@ class ReverseString
16
16
* @param str String to be reversed
17
17
* @return Reversed string
18
18
*/
19
- static String reverseString (String str )
20
- {
21
- String reverse ="" ;
22
- if (str .length ()==1 )
23
- {
24
- return str ;
25
- }
26
- else
27
- {
28
- reverse =reverse +str .charAt (str .length ()-1 )+reverseString (str .substring (0 ,str .length ()-1 ));
29
- return reverse ;
30
- }
31
- }
19
+ public static String reverse (String str ){
20
+ if (str .isEmpty () || str == null ) return str ;
21
+
22
+ char arr [] = str .toCharArray ();
23
+ for (int i = 0 , j = str .length () - 1 ; i < j ; i ++, j --){
24
+ char temp = arr [i ];
25
+ arr [i ] = arr [j ];
26
+ arr [j ] = temp ;
27
+ }
28
+ return new String (arr );
29
+ }
32
30
33
31
/**
34
32
* Main Method
@@ -45,4 +43,4 @@ public static void main(String args[]) throws IOException
45
43
br .close ();
46
44
}
47
45
}
48
-
46
+
You can’t perform that action at this time.
0 commit comments