Skip to content

Commit c910889

Browse files
committed
Grinded some Tries and Neetcode
1 parent 2630fb7 commit c910889

12 files changed

+507
-9
lines changed

README.md

+19-9
Large diffs are not rendered by default.

my-submissions/e206.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode reverseList(ListNode head) {
13+
head = helper(null, head);
14+
return head;
15+
}
16+
private ListNode helper(ListNode prev, ListNode curr) {
17+
if (curr == null) {
18+
return prev;
19+
}
20+
ListNode output = helper(curr, curr.next);
21+
curr.next = prev;
22+
return output;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
class Solution:
2+
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
3+
# Recursive propogation to search for words stemming from a specific index
4+
def propogate(board: List[List[str]], row: int, col: int, currentTrie) -> None :
5+
if not (0 <= row < len(board)) or not (0 <= col < len(board[0])) :
6+
return
7+
if board[row][col] not in currentTrie :
8+
return
9+
10+
currentTrie = currentTrie[board[row][col]]
11+
12+
if False in currentTrie :
13+
self.found.add(currentTrie[False])
14+
15+
newIndices = [(row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1)]
16+
for r, c in newIndices :
17+
if (r, c) not in self.visited and (0 <= r < len(board)) and (0 <= c < len(board[0])):
18+
self.visited.add((r, c))
19+
propogate(board, r, c, currentTrie)
20+
self.visited.remove((r, c))
21+
22+
23+
24+
# Parsing through each possible origin index
25+
self.trie = self.constructTrie(words)
26+
output = set()
27+
28+
for row in range(len(board)) :
29+
for col in range(len(board[0])) :
30+
self.visited = set()
31+
self.found = set()
32+
self.visited.add((row, col))
33+
propogate(board, row, col, self.trie)
34+
35+
# Compiling the words we found and removing from
36+
# our search trie
37+
for fnd in self.found :
38+
self.trie = self.removeWordFromTrie(fnd, self.trie)
39+
output |= self.found
40+
41+
return list(output)
42+
43+
# Helper method to remove a word from our trie
44+
def removeWordFromTrie(self, word: str, trie: dict) -> dict :
45+
curr = trie
46+
curr['Counter'] = curr.get('Counter', 0) - 1
47+
48+
if curr['Counter'] <= 0 :
49+
return {}
50+
51+
for c in word :
52+
if c not in curr :
53+
break
54+
55+
curr[c]['Counter'] = curr[c].get('Counter', 0) - 1
56+
if curr[c]['Counter'] <= 0 :
57+
curr.pop(c)
58+
break
59+
curr = curr[c]
60+
61+
if False in curr :
62+
curr.pop(False)
63+
64+
return trie
65+
66+
67+
# Helper method to create our initial trie of words that we're searching for
68+
def constructTrie(self, words: List[str]) -> dict :
69+
trie = {}
70+
71+
for word in words :
72+
curr = trie
73+
trie['Counter'] = trie.get('Counter', 0) + 1
74+
for c in word :
75+
if c not in curr :
76+
curr[c] = {}
77+
curr = curr[c]
78+
curr['Counter'] = curr.get('Counter', 0) + 1
79+
curr[False] = word
80+
81+
return trie
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution:
2+
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
3+
trie = self.constructTrie(words)
4+
output = set()
5+
visited = set()
6+
def propogate(board: List[List[str]], row: int, col: int, currentTrie: dict) -> None :
7+
if not (0 <= row < len(board)) or not (0 <= col < len(board[0])) :
8+
return
9+
if board[row][col] not in currentTrie :
10+
return
11+
12+
currentTrie = currentTrie[board[row][col]]
13+
14+
if False in currentTrie :
15+
output.add(currentTrie[False])
16+
17+
newIndices = [(row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1)]
18+
19+
for r, c in newIndices :
20+
if (r, c) not in visited and (0 <= r < len(board)) and (0 <= c < len(board[0])):
21+
visited.add((r, c))
22+
propogate(board, r, c, currentTrie)
23+
visited.remove((r, c))
24+
25+
for row in range(len(board)) :
26+
for col in range(len(board[0])) :
27+
visited = set()
28+
visited.add((row, col))
29+
propogate(board, row, col, trie)
30+
31+
return list(output)
32+
33+
def constructTrie(self, words: List[str]) -> dict :
34+
trie = {}
35+
36+
for word in words :
37+
curr = trie
38+
for c in word :
39+
if c not in curr :
40+
curr[c] = {}
41+
curr = curr[c]
42+
curr[False] = word
43+
44+
return trie

my-submissions/m138.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
5+
self.val = int(x)
6+
self.next = next
7+
self.random = random
8+
"""
9+
10+
class Solution:
11+
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
12+
if not head :
13+
return None
14+
15+
origNodes = {}
16+
newNodes = []
17+
18+
newHead = Node(head.val)
19+
20+
origCurr = head
21+
curr = newHead
22+
23+
indx = 0
24+
origNodes[origCurr] = indx
25+
newNodes.append(curr)
26+
27+
28+
while origCurr.next :
29+
indx += 1
30+
origCurr = origCurr.next
31+
origNodes[origCurr] = indx
32+
curr.next = Node(origCurr.val)
33+
curr = curr.next
34+
newNodes.append(curr)
35+
36+
curr = newHead
37+
origCurr = head
38+
39+
while origCurr :
40+
if origCurr.random :
41+
curr.random = newNodes[origNodes.get(origCurr.random)]
42+
43+
origCurr = origCurr.next
44+
curr = curr.next
45+
46+
return newHead
47+

my-submissions/m143 v2 reverse.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public void reorderList(ListNode head) {
13+
ListNode slow = head;
14+
ListNode fast = head;
15+
16+
while (fast.next != null && fast.next.next != null) {
17+
fast = fast.next.next;
18+
slow = slow.next;
19+
}
20+
21+
ListNode prev = slow;
22+
slow = slow.next;
23+
prev.next = null;
24+
while (slow != null) {
25+
ListNode temp = slow.next;
26+
slow.next = prev;
27+
prev = slow;
28+
slow = temp;
29+
}
30+
31+
ListNode front = head.next;
32+
ListNode back = prev;
33+
ListNode newHead = head;
34+
35+
while (front != null && back != null) {
36+
if (back != null) {
37+
newHead.next = back;
38+
back = back.next;
39+
newHead = newHead.next;
40+
}
41+
42+
if (front != null) {
43+
newHead.next = front;
44+
front = front.next;
45+
newHead = newHead.next;
46+
}
47+
}
48+
if (back != null) {
49+
newHead.next = back;
50+
back.next = null;
51+
}
52+
}
53+
}

my-submissions/m1804.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Trie:
2+
3+
def __init__(self):
4+
self.trie = {}
5+
6+
def insert(self, word: str) -> None:
7+
curr = self.trie
8+
for c in word :
9+
if c not in curr :
10+
curr[c] = {}
11+
curr = curr[c]
12+
13+
if False in curr :
14+
curr[False] += 1
15+
else :
16+
curr[False] = 1
17+
18+
def countWordsEqualTo(self, word: str) -> int:
19+
curr = self.trie
20+
for c in word :
21+
if c not in curr :
22+
return 0
23+
curr = curr[c]
24+
25+
return curr.get(False, 0)
26+
27+
def countWordsStartingWith(self, prefix: str) -> int:
28+
curr = self.trie
29+
for c in prefix :
30+
if c not in curr :
31+
return 0
32+
curr = curr[c]
33+
return self.countAllFromHere(curr)
34+
35+
def countAllFromHere(self, trie: dict) -> int :
36+
output = trie.get(False, 0)
37+
38+
for k in trie :
39+
if k :
40+
output += self.countAllFromHere(trie[k])
41+
42+
return output
43+
44+
def erase(self, word: str) -> None:
45+
curr = self.trie
46+
for c in word :
47+
if c not in curr :
48+
return
49+
curr = curr[c]
50+
51+
curr[False] = curr.get(False, 1) - 1
52+
53+
54+
# Your Trie object will be instantiated and called as such:
55+
# obj = Trie()
56+
# obj.insert(word)
57+
# param_2 = obj.countWordsEqualTo(word)
58+
# param_3 = obj.countWordsStartingWith(prefix)
59+
# obj.erase(word)

my-submissions/m208.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Trie:
2+
3+
def __init__(self):
4+
self.trie = {}
5+
6+
def insert(self, word: str) -> None:
7+
curr = self.trie
8+
9+
for c in word :
10+
if c in curr :
11+
curr = curr[c]
12+
else :
13+
curr[c] = {}
14+
curr = curr[c]
15+
16+
curr['#'] = True
17+
18+
def search(self, word: str) -> bool:
19+
curr = self.trie
20+
for c in word :
21+
if c not in curr :
22+
return False
23+
curr = curr[c]
24+
25+
if '#' in curr :
26+
return True
27+
28+
return False
29+
30+
def startsWith(self, prefix: str) -> bool:
31+
curr = self.trie
32+
for c in prefix :
33+
if c not in curr :
34+
return False
35+
curr = curr[c]
36+
return True
37+
38+
39+
# Your Trie object will be instantiated and called as such:
40+
# obj = Trie()
41+
# obj.insert(word)
42+
# param_2 = obj.search(word)
43+
# param_3 = obj.startsWith(prefix)

0 commit comments

Comments
 (0)