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(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
if s is '':
return True
import string
valid = set(string.ascii_letters + string.digits)
s_cp = s[:].lower()
left, right = 0, len(s) - 1
while left <= right:
if s_cp[left] not in valid:
left += 1
elif s_cp[right] not in valid:
right -= 1
elif s_cp[left] != s_cp[right]:
return False
else:
left += 1
right -= 1
return True
class Solution {
public boolean isPalindrome(String s) {
if (s.length() == 0) {
return true;
}
char[] charArr = s.toCharArray();
int left = 0, right = s.length() - 1;
while (left < right) {
if (!Character.isLetterOrDigit(charArr[left])) {
left += 1;
} else if (!Character.isLetterOrDigit(charArr[right])) {
right -= 1;
} else if (Character.toUpperCase(charArr[left]) != Character.toUpperCase(charArr[right])) { return false;
} else {
left += 1;
right -= 1;
}
}
return true;
}
}

最新文章

  1. 360急速浏览器BUG,POST表单提交参数丢失
  2. js问题解释
  3. iOS-XMPP客户端
  4. NSPredicate,谓词
  5. 关于java.sql.SQLRecoverableException: Closed Connection异常的解决方案(转)
  6. Swift编程语言学习4.1——周期
  7. angularjs中动态为audio绑定src
  8. 1、手把手教你Extjs5(一)搭建ExtJS5环境
  9. 一篇文章让你搞懂 SSL 证书
  10. C#快速入门
  11. 使用stringstream对象简化类型转换
  12. BZOJ.5305.[HAOI2018]苹果树(组合 计数)
  13. 浏览器兼容html头部&lt;meta&gt;标签主要内容详情
  14. 网络-01-端口号-linux端口详解大全
  15. VS2017无法进入安装界面问题的解决方法
  16. zabbix添加监控Mysql
  17. Linux基础学习(1)--Linux系统简介
  18. [LeetCode&amp;Python] Problem 599. Minimum Index Sum of Two Lists
  19. War3编辑器
  20. Linux中Postfix邮件认证配置(五)

热门文章

  1. Vue-router(1)之component标签
  2. 使用linux服务器安装wordpress博客详细教程
  3. Iptables的规则语法
  4. 【MySQL参数】-innodb_additional_mem_pool_size
  5. 01 语言基础+高级:1-2 面向对象和封装_day06【类与对象、封装、构造方法】
  6. node 第三方库总结
  7. ZJNU 2201 - 挖矿谷物语
  8. tensorflow object detection api android
  9. Filezilla Xshell SecureFX Win10等无法拖放文件(本地或线上)解决办法
  10. Office文档WEB端在线浏览(转换成Html)