-
Notifications
You must be signed in to change notification settings - Fork 43
/
valid-palindrome-ii.py
172 lines (142 loc) · 4.33 KB
/
valid-palindrome-ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
680. Valid Palindrome II
Easy
Given a string s, return true if the s can be palindrome after deleting at most one character from it.
Example 1:
Input: s = "aba"
Output: true
Example 2:
Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.
Example 3:
Input: s = "abc"
Output: false
Constraints:
1 <= s.length <= 105
s consists of lowercase English letters.
"""
# V0
class Solution:
def validPalindrome(self, s):
l, r = 0, len(s) - 1
while l < r:
if s[l] != s[r]:
"""
# NOTE this !!!!
-> break down the problem to even, odd cases
"""
even, odd = s[l:r], s[l+1:r+1]
# NOTE this !!!!
return even == even[::-1] or odd == odd[::-1]
else:
l += 1
r -= 1
return True
# V1
# IDEA : 2 pointers + array op
# https://leetcode.com/problems/valid-palindrome-ii/discuss/469677/JavaScript-and-Python-Solution
class Solution:
def validPalindrome(self, s: str) -> bool:
l, r = 0, len(s) - 1
while l < r:
if s[l] != s[r]:
"""
# NOTE this !!!!
-> break down the problem to even, odd cases
"""
even, odd = s[l:r], s[l+1:r+1]
# NOTE this !!!!
return even == even[::-1] or odd == odd[::-1]
else:
l += 1
r -= 1
return True
# V1'
# IDEA : 2 pointers + array op
# https://leetcode.com/problems/valid-palindrome-ii/discuss/107723/Super-Simple-Python-Solution
class Solution(object):
def validPalindrome(self, s):
i = 0
j = len(s)-1
while i <= j:
if s[i] == s[j]:
i += 1
j -= 1
else:
return s[i:j] == s[i:j][::-1] or s[i+1:j+1] == s[i+1:j+1][::-1]
return True
# V1''
# IDEA : 2 pointers + RECURSIVE
# https://leetcode.com/problems/valid-palindrome-ii/discuss/1123875/Python
class Solution:
def validPalindrome(self, s: str) -> bool:
isPalindrome = lambda x: x == x[:: -1]
left, right = 0, len(s) - 1
while left < right:
if s[left] == s[right]:
left += 1
right -= 1
else:
return isPalindrome(s[left: right]) or isPalindrome(s[left + 1: right + 1])
return True
# V1'''
# IDEA : Two Pointers
# https://leetcode.com/problems/valid-palindrome-ii/submissions/
class Solution:
def validPalindrome(self, s: str) -> bool:
def check_palindrome(s, i, j):
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
i = 0
j = len(s) - 1
while i < j:
# Found a mismatched pair - try both deletions
if s[i] != s[j]:
return check_palindrome(s, i, j - 1) or check_palindrome(s, i + 1, j)
i += 1
j -= 1
return True
# V1''''
# https://leetcode.com/problems/valid-palindrome-ii/discuss/1410354/Recursive-Python
class Solution:
def validPalindrome(self, s: str) -> bool:
if not s:
return True
def isPalindrome(l, r, edit_used):
while l <= r and s[l] == s[r]:
l += 1
r -= 1
if l < r and not edit_used:
return isPalindrome(l+1, r, True) or isPalindrome(l, r-1, True)
elif l < r:
return False
else:
return True
return isPalindrome(0, len(s)-1, False)
# V1'''''
# https://leetcode.com/problems/valid-palindrome-ii/discuss/219079/Simple-Python-Solution
class Solution:
def isValid(self, s):
try:
while s[-1] == s[0]:
s.pop(-1)
s.pop(0)
except:
pass
return len(s) == 0
def validPalindrome(self, s):
s = list(s)
if s[::-1] == s:
return True
while s[-1] == s[0]:
s.pop(-1)
s.pop(0)
if len(s) > 1:
return self.isValid(list(s)[1:]) or self.isValid(list(s)[:-1])
return True
# V2