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
/**
*
* @author dongb
* Valid Palindrome
* TC O(n), SC O(1)
*/
public class Solution {
public static boolean isPalindrome(String s) {
if (s.length() == 0)
return true;
s = s.toLowerCase();
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (!Character.isLetterOrDigit(s.charAt(left)))
left++;
else if (!Character.isLetterOrDigit(s.charAt(right)))
right--;
else if (s.charAt(left) != s.charAt(right))
return false;
else {
left++;
right--;
}
}
return true;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String s = cin.nextLine();
boolean answer = isPalindrome(s);
System.out.println(answer);
}
}