forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAirbnb Interview Boggle.py
More file actions
219 lines (188 loc) · 7.08 KB
/
Airbnb Interview Boggle.py
File metadata and controls
219 lines (188 loc) · 7.08 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
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
# -*- coding: cp1252 -*-
# Boggle
'''
Given dictionary of words, and a board.
You need to find largest set of words on the board that are present in dictionary such that:
1)
Words should be created by using adjoining letters,
The letters may join in any direction – horizontally, vertically, or diagonally.
Words can be spelled in any direction, including backwards.
2)
You may not use a letter cube multiple times in a single word.
3)
Once you find a word, you may not use the same cubes of the letters in the word to find other words.
Example:
Input:
board = [
['o', 'a', 'a', 'n'],
['e', 't', 'a', 'e'],
['i', 'h', 'k', 'r'],
['i', 'f', 'l', 'v']
]
wordList = ["eat", "oath", "aak", "ner", "oei", "thfl"]
Output:
["aak", "ner", "oei", "thfl"]
Explanation:
Actually all these words can be formed in the matrix, but we have to ensure the biggest list of words.
'''
#Approch:
# Please go through Word Search II to find out most of the logic.
class TrieNode(object):
def __init__(self, letter):
self.letter = letter
self.next = {}
# this helps in determining end of word
self.word = ""
class Trie(object):
def __init__(self):
self.root = TrieNode("")
def createNode(self, letter):
return TrieNode(letter)
def addWord(self, word):
ptr = self.root
for char in word:
if not(char in ptr.next):
ptr.next[char] = self.createNode(char)
ptr = ptr.next[char]
ptr.word = word
def dfs(self, node):
if(node.word != ""):
print node.word
for n in node.next:
self.dfs(node.next[n])
def display(self):
self.dfs(self.root)
class BoggleTrieDfs(object):
def __init__(self):
# key is word value is [board coordinates]
self.hash = {}
self.trie = Trie()
self.maxWordList = []
def isValid(self, row, col, board):
if(row >= 0 and row < len(board)):
if(col >= 0 and col < len(board[0])):
return True
def findWordsOnBoard(self, board, row, col, trieNode, visited, coordinates):
# if we find a word add the coordinates of that to hash table
if(trieNode.word != ""):
self.hash[trieNode.word].append(coordinates)
# continue the search
# we are moving, up, down, left, right, and diagonally up left, diagonally up right, diagonally down left, diagonally down right
moves = [[0, 1], [0, -1], [1, 0], [-1, 0], [-1, -1], [-1, 1], [1, -1], [1, 1]]
for move in moves:
new_r = move[0]+row
new_c = move[1]+col
# if new row is valid and not visited
if(self.isValid(new_r, new_c, board) and visited[new_r][new_c] == False):
# if the valid board cell letter is present in trie node next
if not(board[new_r][new_c] in trieNode.next):
continue
visited[new_r][new_c] = True
# please pay special attention to trieNode.next[board[new_r][new_c]]
# here we are sending the trie node who's letter matches with that of board[new_r][new_c]
self.findWordsOnBoard(board, new_r, new_c, trieNode.next[board[new_r][new_c]], visited, coordinates+[[new_r, new_c]])
# backtrack
visited[new_r][new_c] = False
def findWords(self, board):
# here is where we do things a little differently from word search II
# when we find a word on the board, we also find it's coordinates and we store thos coordinates in hash table
# so now let's start finding the words on the board from each point
for row in range(len(board)):
for col in range(len(board[0])):
# if current letter of the board is in trie
if not(board[row][col] in self.trie.root.next):
continue
visited = [[False for c in range(len(board[0]))] for r in range(len(board))]
visited[row][col] = True
# please pay special attention to self.trie.root.next[board[row][col]]
# here we are sending the node who's letter matches with that of board[row][col]
self.findWordsOnBoard(board, row, col, self.trie.root.next[board[row][col]], visited, [[row, col]])
def coordinatesPresent(self, coordinates, outputCoordinates):
for coordinate in coordinates:
if(coordinate in outputCoordinates):
return True
return False
def findMaxListOfWords(self, wordList, index, wordOutput, outputCoordinates):
if(index == len(wordList)):
if(len(wordOutput) > len(self.maxWordList)):
self.maxWordList = wordOutput[:]
return
for i in range(index, len(wordList)):
for coordinates in self.hash[wordList[i]]:
# if coordinates not present in output coordinates
if not(self.coordinatesPresent(coordinates, outputCoordinates)):
self.findMaxListOfWords(wordList, i+1, wordOutput+[wordList[i]], outputCoordinates+coordinates)
def getMaxWordOnBoard(self, board, wordList):
# add all words in word list to trie
for word in wordList:
self.trie.addWord(word)
# initialize the hash table
self.hash = {word:[] for word in wordList}
#self.trie.display()
self.findWords(board)
#print self.hash
self.findMaxListOfWords(wordList, 0, [], [])
print self.maxWordList
# Main
board = [
['o', 'a', 'a', 'n'],
['e', 't', 'a', 'e'],
['i', 'h', 'k', 'r'],
['i', 'f', 'l', 'v']
]
wordList = ["eat", "oath", "aak", "ner", "oei", "thfl"]
b = BoggleTrieDfs()
b.getMaxWordOnBoard(board, wordList)
board = [
["a", "x", "e"],
["s", "e", "l"],
["s", "h", "o"]
]
wordList = ["asshole", "ass", "she", "ex", "hole"]
b = BoggleTrieDfs()
b.getMaxWordOnBoard(board, wordList)
board=[
["o","a","a","n"],
["e","t","a","e"],
["i","h","k","r"],
["i","f","l","v"]
]
wordList=["oath","pea","eak","rain","ifl"]
b = BoggleTrieDfs()
b.getMaxWordOnBoard(board, wordList)
board=[
["a","b","c"],
["d","e","f"],
["g","h","i"]
]
wordList=["abc", "cfi", "beh", "defi", "gh"]
b = BoggleTrieDfs()
b.getMaxWordOnBoard(board, wordList)
board=[
["a","b"],
["c","d"],
["a","b"],
["c","d"]
]
wordList=["ab","ac","acd","c","d"]
b = BoggleTrieDfs()
b.getMaxWordOnBoard(board, wordList)
board=[
["a","a","b","a"],
["a","b","a","a"],
["a","a","a","b"],
["b","a","a","a"]
]
wordList=["aba","ba","baba","abab", "ab", "b"]
b = BoggleTrieDfs()
b.getMaxWordOnBoard(board, wordList)
# this takes too much time.
board=[
["a","a","a","a"],
["a","a","a","a"],
["a","a","a","a"],
["a","a","a","a"]
]
wordList=["aaa","aa","aaaa","aaaa", "aa", "a"]
b = BoggleTrieDfs()
b.getMaxWordOnBoard(board, wordList)