write an algorithm to decide weather a string is a palindrome.
Ignore any non-letter characters in the the string.
Ignore capital/lower case.
Space complexity O(1)
for example, the following should return true:
Ignore any non-letter characters in the the string.
Ignore capital/lower case.
Space complexity O(1)
for example, the following should return true:
A man, a plan, a canal -- Panama!
public boolean isPalindrome(char[] chars) {
int start = 0, end = chars.length - 1;
while (start < end) {
if (!isLetter(chars[start])) {
start++;
} else if (!isLetter(chars[end])) {
end--;
} else {
if (chars[start] == chars[end]
|| Math.abs(chars[start] - chars[end]) == 'a' - 'A') {
start++;
end--;
} else {
return false;
}
}
}
return true;
}
private boolean isLetter(char c) {
return (c > 'a' && c < 'z') || (c > 'A' && c < 'Z');
}