Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
代码
publicclassSolution {publicbooleanisPalindrome(String s) {// 1. input validationif (s ==null||s.trim().isEmpty()) returntrue;// 2. Filter punctuation character int l =0;int r =s.length() -1;while (l < r) {// 先过滤if (!Character.isLetterOrDigit(s.charAt(l))) { l++;continue; }if (!Character.isLetterOrDigit(s.charAt(r))) { r--;continue; }// 判断 left 和 rightif (Character.toLowerCase(s.charAt(l)) ==Character.toLowerCase(s.charAt(r))) { l++; r--; } else {returnfalse; } }returntrue; }}