-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsPalindrome.java
50 lines (43 loc) · 1.43 KB
/
IsPalindrome.java
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
public class IsPalindrome {
public static boolean isPalRecursive(String str) {
if (str.length() == 0 || str.length() == 1) { // base
return true;
}
if (str.charAt(0) == str.charAt(str.length() - 1)) {
return isPalRecursive(str.substring(1, str.length() - 1));
}
return false;
}
public static boolean isPalIterative(String str) {
int a = 0;
int b = str.length() - 1;
while (b > a) {
if (str.charAt(a) != str.charAt(b)) {
return false;
}
a++;
b--;
}
return true;
}
public static boolean isPalInteger(int x) {
String str = String.valueOf(x);
int n = str.length();
for (int i = 0; i < n/2; i++) {
if (str.charAt(i) != str.charAt(n - i - 1)) return false;
}
return true;
}
public static void main(String[] args) {
String exampleTrue = "radar";
String exampleFalse = "word";
int exampleTrueInt = 121;
int exampleFalseInt = 489;
System.out.println(isPalRecursive(exampleTrue));
System.out.println(isPalRecursive(exampleFalse));
System.out.println(isPalIterative(exampleTrue));
System.out.println(isPalIterative(exampleFalse));
System.out.println(isPalInteger(exampleTrueInt));
System.out.println(isPalInteger(exampleFalseInt));
}
}