-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemsOnFunctionalRecursion.java
More file actions
67 lines (46 loc) · 1.72 KB
/
Copy pathProblemsOnFunctionalRecursion.java
File metadata and controls
67 lines (46 loc) · 1.72 KB
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
61
62
63
64
65
66
67
public class ProblemsOnFunctionalRecursion {
public static int[] reverseArrayRecursion(int[] arr,int s,int e){ // swap using two variables
if(s>=e){
return arr;
}
int temp;
temp = arr[s];
arr[s] = arr[e];
arr[e] = temp;
return reverseArrayRecursion(arr, s+1, e-1);
}
public static int[] reverseArrayRecursion1pointer(int[] arr,int i){ // swap using two variables
if(i >= arr.length/2 ){
return arr;
}
int temp;
temp = arr[i];
arr[i] = arr[(arr.length - 1) - i];
arr[((arr.length - 1) - i)] = temp;
return reverseArrayRecursion1pointer(arr, i+1);
}
public static boolean isPalindrome(String s,int i){ // TC = O(n) , SC = O(n/2)// due to recursive stack space
// n/2 because we are checking only half of the string
if (i >= s.length() / 2) {
return true;
}
if(s.charAt(i) != s.charAt((s.length()-1)-i )) {
return false;
}
return isPalindrome(s,i+1);
}
public static void main(String[] args) {
// int[] arr = { 2,4,3,5,0};
// for(int i = 0; i < arr.length;i++){
// System.out.print(arr[i] + " ");
// }
//int[] revArr = reverseArrayRecursion1pointer(arr,0);
// System.out.println("");
// for (int idx = 0; idx < arr.length; idx++) {
// System.out.print(revArr[idx] + " ");
// }
// System.out.println("");
String s ="MADAM";
System.out.println(isPalindrome(s,0));
}
}