-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursivePalindrome.java
More file actions
40 lines (26 loc) · 1.15 KB
/
RecursivePalindrome.java
File metadata and controls
40 lines (26 loc) · 1.15 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
// Christopher Yonek
// Compute Palindrome (Recursion)
public class RecursivePalindrome {
public static boolean isPalindrome(String s) {
return isPalindrome(s, 0, s.length() - 1);
}
public static boolean isPalindrome(String s, int low, int high) {
if (high <= low) // Base case
return true;
else if (s.charAt(low) != s.charAt(high)) // Base case
return false;
else
return isPalindrome(s, low + 1, high - 1);
}
public static void main(String[] args) {
String[] palinList = {"I","kk","wow","anna","madam","poppop",
"racecar", "snellens","evitative","jjiijjiijj","aibohphobia",
"tattarrattat","lolololololol","ccddccddccddcc","yoyoyoyoyoyoyoy",
"xxxxxxxxxxxxxxxx","vevevevevevevevev","yyyyyyyyyyyyyyyyyy",
"zzzzzzzzzzzzzzzzzzz"};
long startTime = System.nanoTime();
System.out.print(isPalindrome(palinList[0]) + " ");
long elapsedTime = System.nanoTime() - startTime;
System.out.println(elapsedTime);
}
}