LeetCode 125. Valid Palindrome
Description
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring other 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.
class Solution {
public boolean isPalindrome(String s) {
if (s.isEmpty()) {
return true;
}
int lo = 0;
int hi = s.length() - 1;
char clo, chi;
while (lo <= hi) {
clo = s.charAt(lo);
chi = s.charAt(hi);
if (!Character.isLetterOrDigit(clo)) {
lo++;
}
else if (!Character.isLetterOrDigit(chi)) {
hi--;
}
else {
if (Character.toLowerCase(clo) != Character.toLowerCase(chi)) {
return false;
}
lo++;
hi--;
}
}
return true;
}
}