Title: |
Valid palindrome |
Pass Rate: |
21.9% |
Difficulty: |
Simple |
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:
Are you consider that the string might is empty? This was a good question to ask during a interview.
For the purpose of this problem, we define empty string as valid palindrome.
Before doing a palindrome number of judgments, palindrome number than this topic more difficult, because palindrome number can not use extra space, if let the extra space becomes the subject, the problem is to remove punctuation, and to ignore the case, then use Java is very simple. It's OK to replace all non-alphanumeric letters with regular expressions to null. Then it is from both ends to judge, as long as it is not equal to return false, if not the first function, then the implementation of a judgment is an alphanumeric function and a case conversion function can be directly read the code:
1 Public classSolution {2 Public BooleanIspalindrome (String s) {3 intLen=s.replaceall ("[^a-za-z0-9]", "" "). Length ();4String Tmp=s.replaceall ("[^a-za-z0-9]", ""). toLowerCase (); 5 if(len==0)return true;6 for(inti=0;i<len/2;i++){7 if(Tmp.charat (i)!=tmp.charat (len-1-i))return false;8 }9 return true;Ten One } A}
Leetcode------Valid Palindrome