2

【leetcode】680.验证回文字符串Ⅱ

 2 years ago
source link: https://blog.feelyou.top/posts/leetcode680.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【leetcode】680.验证回文字符串Ⅱ

2020-03-07

链接:https://leetcode-cn.com/problems/valid-palindrome-ii

给定一个非空字符串 s,最多删除一个字符。判断是否能成为回文字符串。

示例 1:

输入: “aba”
输出: True

示例 2:

输入: “abca”
输出: True
解释: 你可以删除c字符。

注意:

  • 字符串只包含从 a-z 的小写字母。字符串的最大长度是50000。

python3

class Solution:
def validPalindrome(self, s: str) -> bool:
if s == s[::-1]:
return True
left, right = 0, len(s)-1
while left < right:
if s[left] == s[right]:
left, right = left+1, right-1
else:
a = s[left+1 : right+1]
b = s[left : right]
return a == a[::-1] or b == b[::-1]

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK