-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.py
99 lines (74 loc) · 1.8 KB
/
palindrome.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
# 2.6: Palindrome
from linked_list import LinkedList, Node
# Runtime: O(n) - Space: O(1)
def get_values(node: Node) -> tuple:
size = 0
counts = {}
while node:
size += 1
counts.update({node.data: counts.get(node.data, 0) + 1})
node = node.next
return size, counts
def palindrome(node: Node) -> bool:
size, counts = get_values(node)
divergences = 0
even = size % 2 == 0
# if even, counts must be divisible by 2
# if odd, there's one difference allowed
for c in counts:
odd = counts[c] % 2 != 0
if even:
if odd:
return False
continue
if odd:
divergences += 1
if divergences > 1:
return False
return True
l1 = LinkedList()
l1.add("r")
l1.add("a")
l1.add("c")
l1.add("e")
l1.add("c")
l1.add("a")
l1.add("r")
l2 = LinkedList()
l2.add("r")
l2.add("a")
l2.add("e")
l2.add("b")
l3 = LinkedList()
l3.add("c")
l3.add("i")
l3.add("v")
l3.add("i")
l3.add("c")
print(palindrome(l1.head)) # True
print(palindrome(l2.head)) # False
print(palindrome(l3.head)) # True
# Runtime: O(n) - Space: O(n/2)
def palindrome2(node: Node) -> bool:
slow = node
fast = node
stack = []
# once fast/fast.next are null, slow is at middle
# push elements from first half of ll to stack
while fast and fast.next:
stack.append(slow.data)
slow = slow.next
fast = fast.next.next
# uneven size, skip the one pivot element
if fast:
slow = slow.next
while slow:
top = stack.pop()
if top != slow.data:
return False
slow = slow.next
return True
print("Chapter 2.6 v2")
print(palindrome2(l1.head)) # True
print(palindrome2(l2.head)) # False
print(palindrome2(l3.head)) # True