Skip to content

Commit a98be0d

Browse files
committedDec 12, 2020
feat: valid-palindrome-ii
1 parent dd6c1c0 commit a98be0d

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed
 

‎str.find-all-anagrams-in-a-string.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class Solution:
44
"""
55
438. 找到字符串中所有字母异位词
6-
https://leetcode-cn.com/problems/strstr/
6+
https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/
77
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引。
88
字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100。
99
说明:
@@ -44,6 +44,7 @@ def findAnagrams(self, s: str, p: str) -> List[int]:
4444
right += 1
4545
return res
4646

47+
# 暴力简单
4748
def findAnagramsByForce(self, s: str, p: str) -> List[int]:
4849
res = []
4950
p_str = ''.join(sorted(p))
@@ -53,9 +54,7 @@ def findAnagramsByForce(self, s: str, p: str) -> List[int]:
5354
tmp = s[i:i+window]
5455
if ''.join(sorted(tmp)) == p_str:
5556
res.append(i)
56-
5757
i += 1
58-
5958
return res
6059

6160

‎str.valid-palindrome-ii.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
"""
3+
680. 验证回文字符串 Ⅱ
4+
https://leetcode-cn.com/problems/valid-palindrome-ii/
5+
给定一个非空字符串 s,最多删除一个字符。判断是否能成为回文字符串。
6+
"""
7+
def validPalindrome(self, s: str) -> bool:
8+
i = 0
9+
j = len(s) - 1
10+
res = 0
11+
while i < j:
12+
# 如果相等,则双指针内移一位
13+
if s[i] == s[j]:
14+
i += 1
15+
j -= 1
16+
else:
17+
if res == 0:
18+
i += 1
19+
res += 1
20+
elif res == 1:
21+
i -= 1
22+
j -= 1
23+
res += 1
24+
else:
25+
return False
26+
27+
return True
28+
29+
30+
so = Solution()
31+
print(so.validPalindrome("aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga"))

0 commit comments

Comments
 (0)