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 boolean isPalindrome(String s) {
        if (s.equals("")) return true;
        String s1 = "";
        s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        char[] ch = s.toCharArray();
        for (int i = ch.length - 1; i >= 0; i--) {
            s1 = s1 + ch[i];
        }
        if (s1.equals(s)) {
            return Boolean.TRUE;
        }
        return false;
    }
}

标签: leetcode, java

添加新评论