Skip to content

Commit 8b4b9fb

Browse files
committed
Added hard problem
1 parent f3e8b74 commit 8b4b9fb

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

Hard/inverted-bisection.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
# INVERTED BISECTION
3+
4+
# This is an input class. Do not edit.
5+
class LinkedList:
6+
def __init__(self, value):
7+
self.value = value
8+
self.next = None
9+
10+
11+
def invertedBisection(head):
12+
# Write your code here.
13+
if head is None or head.next is None:
14+
return head
15+
16+
prev = None
17+
slow = head
18+
fast = head
19+
while fast and fast.next:
20+
prev = slow
21+
slow = slow.next
22+
fast = fast.next.next
23+
24+
hasEvenCount = True if fast == None else False
25+
prev.next = None
26+
if hasEvenCount:
27+
firstHalf = reverseLinkedList(head)
28+
secondHalf = reverseLinkedList(slow)
29+
head.next = secondHalf
30+
return firstHalf
31+
else:
32+
firstHalf = reverseLinkedList(head)
33+
secondHalf = reverseLinkedList(slow.next)
34+
head.next = slow
35+
slow.next = secondHalf
36+
return firstHalf
37+
38+
def reverseLinkedList(head):
39+
prev = None
40+
cur = head
41+
while cur is not None:
42+
temp = cur.next
43+
cur.next = prev
44+
prev = cur
45+
cur = temp
46+
47+
return prev
48+

Hard/special-strings.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
# SPECIAL STRINGS
3+
4+
def specialStrings(strings):
5+
# Write your code here.
6+
trie = Trie()
7+
for string in strings:
8+
trie.buildTrie(string)
9+
10+
specials = []
11+
for string in strings:
12+
if checkSpecial(string, trie.root, 0, 0, trie):
13+
specials.append(string)
14+
15+
return specials
16+
17+
def checkSpecial(string, node, index, count, trie):
18+
letter = string[index]
19+
if letter not in node:
20+
return False
21+
22+
if index == len(string) - 1:
23+
return trie.endSymbol in node[letter] and count > 0
24+
25+
if trie.endSymbol in node[letter]:
26+
remainingSpecial = checkSpecial(string, trie.root, index + 1, count + 1, trie)
27+
if remainingSpecial:
28+
return True
29+
30+
return checkSpecial(string, node[letter], index + 1, count, trie)
31+
32+
class Trie:
33+
def __init__(self):
34+
self.root = {}
35+
self.endSymbol = "*"
36+
37+
def buildTrie(self, string):
38+
current = self.root
39+
for letter in string:
40+
if letter not in current:
41+
current[letter] = {}
42+
current = current[letter]
43+
current[self.endSymbol] = True

0 commit comments

Comments
 (0)