-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125_Valid_Palindrome.py
More file actions
64 lines (53 loc) · 1.71 KB
/
125_Valid_Palindrome.py
File metadata and controls
64 lines (53 loc) · 1.71 KB
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
# 4 Possible Solutions
# 1. Compare with Reverse - String
# 2. Compare with Reverse - List
# 3. Recursion
# 4. Two Pointers
class Solution(object):
# Time: O(N^2), Space: O(N)
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
reversedString = ""
s = s.lower()
for character in s:
if character.isalnum():
# String Concat = N * N
# Concat requires iterating over string 'reversedString' every loop before appending,thus the complexity.
reversedString += character.lower()
return reversedString == reversedString[::-1]
# Time: O(N), Space: O(N)
def isPalindrome(self, s):
reversedChars = []
for i in range(len(s)):
reversedChars.append(s[i])
return s == "".join(reversedChars)
# Time: O(N), Space: O(N)
def isPalindrome(self, s, i = 0):
j = len(s) - 1 - i
return True if i >= j else s[i] == s[j] and self.isPalindrome(s, i + 1)
# Time: O(N), Space: O(1)
def isPalindrome(self, s):
leftIdx = 0
rightIdx = len(s) - 1
s = s.lower()
while leftIdx < rightIdx:
if s[leftIdx].isalnum() == False:
leftIdx += 1
elif s[rightIdx].isalnum() == False:
rightIdx -= 1
elif s[leftIdx] != s[rightIdx]:
return False
else:
leftIdx += 1
rightIdx -= 1
return True
def printListString(self, s):
indexValue = 3
#print(s[indexValue])
soln = Solution()
s = "A man, a plan, a canal: Panama"
print(soln.isPalindrome(s))
print(soln.printListString(s))