125. Valid Palindrome
Easy
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
package leetcode.easy; public class ValidPalindrome { @org.junit.Test public void test() { System.out.println(isPalindrome("A man, a plan, a canal: Panama")); System.out.println(isPalindrome("race a car")); } public boolean isPalindrome(String s) { s = s.trim().toLowerCase(); char[] chs = s.toCharArray(); int count = 0; for (int i = 0; i < chs.length; i++) { if ((chs[i] >= '0' && chs[i] <= '9') || (chs[i] >= 'a' && chs[i] <= 'z')) { chs[count] = chs[i]; count++; } } for (int i = 0; i < count / 2; i++) { if (chs[i] != chs[count - i - 1]) { return false; } } return true; } }