Validate Palindrome 验证回文字符串

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
  
"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.

验证回文字符串是比较常见的问题,所谓回文,就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。但是这里,加入了空格和非字母数字的字符,增加了些难度,但其实原理还是很简单:只需要建立两个指针,left和right, 分别从字符的开头和结尾处开始遍历整个字符串,如果遇到非字母数字的字符就跳过,继续往下找,直到找到下一个字母数字或者结束遍历,如果遇到大写字母,就将其转为小写。等左右指针都找到字母数字时,比较这两个字符,若相等,则继续比较下面两个分别找到的字母数字,若不相等,直接返回false.

时间复杂度为O(n), 代码如下:

class Solution {
public:
    bool isPalindrome(string s) {
        if (s.length() < 2) return true;

        int left = 0, right = s.length() - 1 ;
        while (left <= right) {
            while (left < s.length() - 1 && !isAlphaNumber(s[left])) ++left;
            while (right >= 0 && !isAlphaNumber(s[right])) --right;
            if (s[left] != s[right] && left <= right) return false;
            ++left;
            --right;
        }
        return true;
    }

    bool isAlphaNumber(char &ch) {
        if (ch >= ‘a‘ && ch <= ‘z‘) return true;
        if (ch >= ‘A‘ && ch <= ‘Z‘) {
            ch += 32;
            return true;
        }
        if (ch >= ‘0‘ && ch <= ‘9‘) return true;
        return false;
    }

};

对于该问题的扩展,还有利用Manacher算法来求解最长回文字符串问题,参见下面链接,有时间可以研究一下。。。

http://blog.csdn.net/yzl_rex/article/details/7908259

时间: 2025-01-19 23:10:10

Validate Palindrome 验证回文字符串的相关文章

[LintCode] Valid Palindrome 验证回文字符串

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

Leetcode 680.验证回文字符串

验证回文字符串 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca" 输出: True 解释: 你可以删除c字符. 注意: 字符串只包含从 a-z 的小写字母.字符串的最大长度是50000. 1 class Solution { 2 public boolean isPalindromeRange(String s, int i, int j) { 3 for (in

验证回文字符串

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 输入: "A man, a plan, a canal: Panama" 输出: true 代码: 思路,这里涉及到了数据清洗,我只要字母和数字,并且字母必须是小写.使用 string,isalnum()可以滤出字母和数字,使用 string.lower()可以滤出小写字母.然后再转换成 list 反转对比即可. http://www.runoob.com/python/python-strings.ht

初级算法-16. 验证回文字符串

题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: false 一种解法是将字符串的有效字符存入数组中,再去比较 1 class Solution { 2 public boolean isPalindrome(Stri

[LeetCode] Palindrome Number 验证回文数字

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

LeetCode(125):验证回文串

Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: false 解题思路: 验证回文字符串是比较常见的问题,所谓回文,就是一个正读和反读都一样的字符串,比如"level"或者"n

131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

131. Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["

LeetCode Valid Palindrome 有效回文(字符串)

1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 if(s=="") return true; 5 if(s.length()==1) return true; //单个字符,对称 6 char *p,*q; 7 p=&s[0]; //p指向开头 8 q=&s[s.length()-1]; //q指向末尾 9 while(p!=q){ 10 //测试字符串里是否有字母或数字,若没有,则立刻返回

409. Longest Palindrome(计算一组字符集合可以组成的回文字符串的最大长度)

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note:Assume the lengt