-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPallindromeCheck_Recursive.java
More file actions
34 lines (29 loc) · 1.23 KB
/
PallindromeCheck_Recursive.java
File metadata and controls
34 lines (29 loc) · 1.23 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
//Time Complexity O(n) | Space Complexity O(n) (Stack used durein recursion)
import java.util.*;
class A
{
public static void main(String[] args)
{
String st = new String("madam");
boolean b = isPallindrome(st);
System.out.println("Given String is "+st);
System.out.println("is it Pallimdrome ? "+b);
}
public static boolean isPallindrome(String st)
{
return isPallindromeHelper(st,0);
}
public static boolean isPallindromeHelper(String st , int firstIdx)
{
int lastIdx = st.length()-1-firstIdx;
//while comparing elements if the index overlap each other or criss cross
//that means given string ia p-allindrome
//comparing elements by picking from both ends together one by one and if they are
//not equal string is not pallindrome
//Here the recursive call is at the absolute end of the program it is called Tail Reucrsion
//Here the function stack space can be replaced one by one in each recursive call instead
//of piling uo over the stack .. so it gives O(1) space instead of the default recursion
//space complexity O(n)
return firstIdx >= lastIdx ? true : st.charAt(firstIdx) == st.charAt(lastIdx) && isPallindromeHelper(st,firstIdx+1);
}
}