File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
2+
3+ For example,
4+ ` "A man, a plan, a canal: Panama" ` is a palindrome.
5+ `"race a car" is * not* a palindrome.
6+
7+ Note:
8+ Have you consider that the string might be empty? This is a good question to ask during an interview.
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ public boolean isPalindrome (String s ) {
3+ if (s == null )
4+ return false ;
5+ if (s .length () == 0 )
6+ return true ;
7+
8+ String sLower = s .toLowerCase ();
9+
10+ int left = 0 ;
11+ int right = sLower .length () - 1 ;
12+
13+ while (left <= right ) {
14+ while (left <= right && !((sLower .charAt (left ) >= 'a' && sLower .charAt (left ) <= 'z' )
15+ || (sLower .charAt (left ) >= '0' && sLower .charAt (left ) <= '9' ))) {
16+ left ++;
17+ }
18+
19+ while (left <= right && !((sLower .charAt (right ) >= 'a' && sLower .charAt (right ) <= 'z' )
20+ || (sLower .charAt (right ) >= '0' && sLower .charAt (right ) <= '9' ))) {
21+ right --;
22+ }
23+
24+ if (left <= right && sLower .charAt (left ) != sLower .charAt (right ))
25+ return false ;
26+
27+ left ++;
28+ right --;
29+ }
30+
31+ return true ;
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments