-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidPalindrome.py
229 lines (184 loc) · 7.28 KB
/
ValidPalindrome.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
#Given a string s, return true if it is a palindrome, or false otherwise.
#Example 1:
#Input: s = "A man, a plan, a canal: Panama"
#Output: true
#Explanation: "amanaplanacanalpanama" is a palindrome.
#Example 2:
#Input: s = "race a car"
#Output: false
#Explanation: "raceacar" is not a palindrome.
#Example 3:
#Input: s = " "
#Output: true
#Explanation: s is an empty string "" after removing non-alphanumeric characters.
#Since an empty string reads the same forward and backward, it is a palindrome.
#My Solution:
class Solution:
def isPalindrome(self, s: str) -> bool:
#DO NOT CONFUSE S = S.REPLACE(CHARACTERTOREPLACE,REPLACEMENT,NUMBEROFTIMESTOREPLACEFROMBEG) WITH S = RE.SUB(PATTERN, REPLACEMENT, STRINGREPLACEMENTISPERFORMEDON)!!!!!
s.replace(" ", "") #s.replace(old, new)
s = re.sub('[\W_]', '', s) #re.sub(pattern, replacement, string). '[\W_]' patternmatches any character that isn't considered a word so that the replacement can replace the pattern.
start, end = 0, len(s) - 1 #indicies representing integers as starting and ending pointer
while start < end: #make sure start and end don't cross
if s[start].casefold() != s[end].casefold(): #if, even after ignoring case, any starting character dosen't matching ending, we can automatically return false and say that it's not a perfect palindrome already
return False
start += 1 #if start and end chars do match, increment start and decrement end and ask the same question until you get to the middle of the string
end -= 1
return True #if, after we've gone through all characters starting on either side of the string and determined that they are all equal, then we do have a valid palindrome, so return True
# 6/9/23 refresher (my solution) - only 94/485 test cases passing - issue is that need to only keep alphanumeric characters and find middle index of string:
class Solution:
def isPalindrome(self, s: str) -> bool #- this approach is starting from the middle and expanding outwards and checking if they are the same until you reach the opposite ends:
middle = len(s) / 2
left, right = middle, middle
while left >= 0 and right < len(s):
if s[left] != s[right]:
return False
left -= 1
right += 1
return True
#6/9/23 refresher (I also tried using a dictionary) - same 94/485 result - the issue is checking for only alphanumeric characters:
class Solution:
def isPalindrome(self, s: str) -> bool:
forwarddict, backwarddict = {}, {}
for i in s:
forwarddict[s] = 0
forwarddict[s] += 1
for j in reversed(s):
backwarddict[j] = 0
backwarddict[j] += 1
if forwarddict == backwarddict:
return True
else:
return False
#12/25/23 refresher:
class Solution:
def isPalindrome(self, s: str) -> bool:
s.replace(" ", "")
s = re.sub('[\W_]', "", s)
l, r = 0, len(s) - 1
#we know the last character is going to be the same as itself, so we don't need =, but having = works too in l <= r is valid too
while l < r:
#starting and ending must be equal because starting is ending's opposite, so if they are the same forward as backward, ending must also be starting's opposite, which they aren't if they aren't equal on both sides
if s[l].casefold() != s[r].casefold():
return False
l += 1
r -= 1
return True
#1/6/24 refresher solution:
s.replace(" ", "")
s = re.sub("[\W_]", "", s)
l, r = 0, len(s) - 1
while l < r:
if s[l].casefold() != s[r].casefold():
return False
l += 1
r -= 1
return True
#1/24/24 refresher:
class Solution:
def isPalindrome(self, s: str) -> bool:
s.replace(" ", "")
s = re.sub("[\W_]", "", s)
l, r = 0, len(s) - 1
while l < r:
if s[l].casefold() != s[r].casefold():
return False
l += 1
r -= 1
return True
#2/3/24 refresher:
#rid whitespaces
s.replace(" ", "")
#get rid of non alphanumeric characters
s = re.sub('[\W_]', "", s)
#we may be working with a smaller length s string now since we got rid of spaces and non alphanumeric characters
l, r = 0, len(s) - 1
while l < r:
if s[l].casefold() != s[r].casefold():
return False
l += 1
r -= 1
return True
#2/16/24:
class Solution:
def isPalindrome(self, s: str) -> bool:
#we want case insensitive comparison
s.replace(" ", "")
#remove all non alphanumeric characters
s = re.sub("[\W_]", "", s)
l, r = 0, len(s) - 1
while l < r:
if s[l].casefold() != s[r].casefold():
return False
l += 1
r -= 1
return True
#3/8/24:
class Solution:
def isPalindrome(self, s: str) -> bool:
#remove all non alphanumeric characters and perform case insensitive check
s = s.replace(" ", "")
s = re.sub('[\W_]', "", s)
l, r = 0, len(s) - 1
while l <= r:
if s[l].casefold() != s[r].casefold():
return False
l += 1
r -= 1
return True
#3/18/24:
class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.replace(" ", "")
s = re.sub('[\W_]', "", s)
l, r = 0, len(s) - 1
while l <= r:
if s[l].casefold() != s[r].casefold():
return False
l += 1
r -= 1
return True
#4/5/24:
class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.replace(" ", "") #solution works even if you remove this line
s = re.sub("[\W_]", "", s)
l, r = 0, len(s) - 1
while l < r:
if s[l].casefold() != s[r].casefold():
return False
l += 1
r -= 1
return True
#note that:
import re
s = "A man, a plan, a canal: Panama"
s = re.sub("[\W_]", "", s)
print(s)
#actually outputs: AmanaplanacanalPanama
#without you even having to replace the spaces
#4/13/24 refresher:
class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.replace(" ", "") #remove spaces
s = re.sub("[\W_]", "", s)
l, r = 0, len(s) - 1
while l <= r:
if s[l].casefold() != s[r].casefold():
return False
l += 1
r -= 1
return True
#5/14/24 refresher:
class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.replace(" ", "") #we don't even need this line because spaces count as non alphanumeric and are removed by re.sub. So re.sub below would remove symbols - this question was missed on 7/27/24!!
s = re.sub("[\W_]", "", s)
l, r = 0, len(s) - 1
while l < r:
if s[l].casefold() != s[r].casefold():
return False
l += 1
r -= 1
return True