Valid Palindrome

来源:互联网 发布:网络词贝塔什么意思 编辑:程序博客网 时间:2024/04/25 19:23

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring 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.


public class Solution {    public boolean isPalindrome(String s) {        if (s == null || s.trim().length() == 0) {return true;}        s = s.toLowerCase();        StringBuilder temp = new StringBuilder();        for (char c : s.toCharArray()) {if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {temp.append(c);}}        return temp.toString().equals(temp.reverse().toString());    }}


0 0