Problem Link:

http://oj.leetcode.com/problems/valid-palindrome/

The following two conditions would simplify the problem:

  • only alphanumerci characters considered
  • ignoring cases

Given a string, we check if it is a valid palindrome by following rules:

  1. An empty string is a valid palindrome;
  2. Letter case should be ignored;
  3. Blank space should be ignored;
  4. The string is a valid palindrome only if s[x] == s[n-x] for x = 0,1,..., n/2

Therefore, we design the algorithm that scan the string from both the beginning and the end. Each iteration, we compare them to check if the string is a valid palindrome so far.

The algorithm will terminate if the two characters are not same; when all the characters in the string are compared, the algorithm will return True.

The following code is the python code accepted by oj.leetcode.com.

class Solution:
# @param s, a string
# @return a boolean
n0 = ord('0')
n9 = ord('9')
A = ord('A')
Z = ord('Z')
def isAlphanumeric(self, c):
x = ord(c)
return ( self.n0 <= x <= self.n9 or \
self.A <= x <= self.Z ) def isPalindrome(self, s):
"""
Scan the string from two ends of the string
"""
low = 0
high = len(s)-1
s = s.upper()
while low <= high:
if not self.isAlphanumeric(s[low]):
low += 1
elif not self.isAlphanumeric(s[high]):
high -= 1
elif s[low] == s[high]:
low += 1
high -= 1
else:
return False
return True

【LeetCode OJ】Valid Palindrome的更多相关文章

  1. 【LeetCode练习题】Valid Palindrome

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ:Valid Palindrome(验证回文)

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  5. 【LeetCode OJ】Palindrome Partitioning

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...

  6. 【LeetCode OJ】Palindrome Partitioning II

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...

  7. 【LeetCode OJ】Gas Station

    Problem link: http://oj.leetcode.com/problems/gas-station/ We can solve this problem by following al ...

  8. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  9. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

随机推荐

  1. 笔记9:winfrom的一些知识点(二)

    一.新建,和删除文件夹 private void button4_Click(object sender, EventArgs e) { Directory.Delete(@"F:\&quo ...

  2. UNIX高级环境编程学习

    1-5实例 控制字符:ctrl + 另一个键.control + D或者^D是默认的文件结束符(EOF字符).

  3. HDU-------(2795)Billboard(线段树区间更新)

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. 3.1 关系数据库标准语言SQL综述

    一.SQL语言的特点 SQL结构查询语言 1.综合统一: 2.高度非过程化:不需要指定存储路径 3.面向集合的操作方式 4.以同一种语法提供两种使用方式:独立语言.嵌入式语言 5.语言简单,易学易用 ...

  5. 使用DD_belatedPNG让IE6支持PNG透明图片

    使用DD_belatedPNG让IE6支持PNG透明图片 众所周知IE6不支持透明的PNG图片,而PNG图片在Web设计方面表现力上,具有其它图形格式所达不到的效果,IE6这一致命缺陷极大地限制了We ...

  6. SVG格式

    SVG格式 编辑 目 录 概述 简介 优势 实例 展现 1概述 SVG格式 SVG是一种用XML定义的语言,用来描述二维矢量及矢量/栅格图形.SVG提供了3种类型的图形对象:矢量图形(vectorgr ...

  7. JDE隐藏Constant等(Hide Object)

    Grid中隐藏列,列值可以使用属性配置,但是列表头Constant需要用函数修改,如下所示:

  8. java入门第二步之helloworld【转】

    前一篇博客已经介绍了jdk的安装:接下来我们就乘热打铁,完成第一个程序:helloworld(每学一样程序的新东西都是从实现helloworld开始的) 1.不是用开发工具IDE,只是使用记事本来实现 ...

  9. JSP 客户端请求

    当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信息头的一部分来传送的.您可以查阅HTTP协议来获得更多的信息. 下表列出了浏览器端信息头的一些重要内 ...

  10. HDU 1681 Frobenius

    题目链接:Frobenius 思路:想了很久还是没转过弯来. 递推. 初始化vis[0] = 1,每次有四种方法扩展,这样能扩展到所有能被表示的数.上界的判定,如果一万以内的数都能被表示,那以后的数肯 ...