leetcode 125. Valid Palindrome
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
class Solution {
public static boolean isPalindrome(String s) {
if(s.length() == 0) return true;
int start = 0;
int end = s.length() -1;
char[] arr = s.toCharArray();
while(end - start >= 1){
if(Character.isLetterOrDigit(arr) && Character.isLetterOrDigit(arr)){
if(Character.toLowerCase(arr) != Character.toLowerCase(arr)) return false;
start++;
end--;
}
else{
if(!Character.isLetterOrDigit(arr)) start++;
if(!Character.isLetterOrDigit(arr)) end--;
}
}
return true;
}
}
页:
[1]