Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama"
is a palindrome.
"race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
1 public class Solution { 2 public static boolean isPalindrome(String s) 3 { 4 char[]ch=s.toLowerCase().toCharArray(); 5 int i=0,j=ch.length-1; 6 while(i<j) 7 { 8 while(i<ch.length&&!(ch[i]>='a'&&ch[i]<='z'||ch[i]>='0'&&ch[i]<='9')) 9 i++; 10 while(j>=0&&!(ch[j]>='a'&&ch[j]<='z'||ch[j]>='0'&&ch[j]<='9')) 11 j--; 12 if(i>ch.length-1||j<0) 13 return true; 14 if(ch[i]!=ch[j]) 15 return false; 16 i++; 17 j--; 18 19 } 20 return true; 21 } 22 public static void main(String args[]) 23 { 24 boolean b=isPalindrome(",."); 25 System.out.println(b); 26 } 27 }