-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (28 loc) · 924 Bytes
/
Copy pathSolution.java
File metadata and controls
39 lines (28 loc) · 924 Bytes
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
package com.ravi.string;
public class Solution {
String revString = "";
public String reverseString(String s) {
char[] chars = s.toCharArray();
for(int i=0; i<chars.length/2;i++){
chars = swap(chars,i);
}
return new String(chars);
}
public char[] swap(char[] chars, int i){
char temp = chars[i];
chars[i] = chars[chars.length - i-1];
chars[chars.length - i-1] = temp;
return chars;
}
public String recReverse(String str){
if(str.length()>0){
revString = str.charAt(str.length()-1)+recReverse(str.substring(0,str.length()-1));
return revString;
}else
return revString;
}
public static void main(String[] args) {
String reverse = new Solution().reverseString("ravi");
System.out.println(reverse);
}
}