-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsPalindrome.java
More file actions
29 lines (22 loc) · 790 Bytes
/
IsPalindrome.java
File metadata and controls
29 lines (22 loc) · 790 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
public class IsPalindrome {
public boolean isPalindrome(String s) {
if(s==null || s.length()<=1) return true;
s = s.toLowerCase();
int r = s.length()-1;
int l = 0;
while(l<r) {
while(l<r && (s.charAt(l)<'a'||s.charAt(l)>'z') && (s.charAt(l)<0 || s.charAt(l)>9)) l++;
while(l<r && (s.charAt(r)<'a'||s.charAt(r)>'z') && (s.charAt(r)<0 || s.charAt(r)>9)) r--;
if(s.charAt(l) != s.charAt(r)) return false;
l++;
r--;
}
return true;
}
public static void main(String[] args) {
IsPalindrome isp = new IsPalindrome();
String test = "1a2";
System.out.println('Z'-'z');
System.out.println(isp.isPalindrome(test));
}
}