Skip to content

Commit b36bebb

Browse files
committed
Leetcode Accepted
1 parent e3604d6 commit b36bebb

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

Blind 75/Programs/Word Search.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Word Search
2+
# https://leetcode.com/problems/word-search/
3+
class Solution(object):
4+
5+
def findIfAllLettersInBoard(self, board, word):
6+
letterHash = {}
7+
# Count all letters on the board and put it in a hash table.
8+
for row in range(len(board)):
9+
for col in range(len(board[0])):
10+
if not (board[row][col] in letterHash):
11+
letterHash[board[row][col]] = 0
12+
letterHash[board[row][col]] = letterHash[board[row][col]] + 1
13+
# see if the word has enough letters that are present on the board
14+
for letter in word:
15+
# all letters from the word are not present on the board
16+
if (letter not in letterHash or letterHash[letter] == 0):
17+
return False
18+
letterHash[letter] = letterHash[letter] - 1
19+
return True
20+
21+
def isValid(self, row, col, board, visited):
22+
if (row >= 0 and row < len(board)):
23+
if (col >= 0 and col < len(board[0])):
24+
if ([row, col] not in visited):
25+
return True
26+
27+
def nextMoves(self, board, row, col, visited):
28+
move = []
29+
# up
30+
if (self.isValid(row-1, col, board, visited)):
31+
move.append([row-1, col])
32+
# down
33+
if (self.isValid(row+1, col, board, visited)):
34+
move.append([row+1, col])
35+
# left
36+
if (self.isValid(row, col-1, board, visited)):
37+
move.append([row, col-1])
38+
# right
39+
if (self.isValid(row, col+1, board, visited)):
40+
move.append([row, col+1])
41+
# return moves
42+
return move
43+
44+
def dfs(self, board, row, col, visited, word, index):
45+
if (index == len(word)):
46+
return True
47+
# get next moves
48+
moves = self.nextMoves(board, row, col, visited)
49+
# iterate through next moves
50+
for move in moves:
51+
# if move has not been visited
52+
if (move not in visited):
53+
# if the cell has same letter as letter at word index
54+
if (board[move[0]][move[1]] == word[index]):
55+
# Mark current row, col visited in the function call, this helps in backtracking.
56+
if (self.dfs(board, move[0], move[1], visited+[[row, col]], word, index+1)):
57+
return True
58+
59+
def exist(self, board, word):
60+
# Edge case to see if all letters in word are present in board
61+
if not(self.findIfAllLettersInBoard(board, word)):
62+
return False
63+
# Search for word in board
64+
for row in range(len(board)):
65+
for col in range(len(board[0])):
66+
if (board[row][col] == word[0]):
67+
if (self.dfs(board, row, col, [], word, 1)):
68+
return True
69+
return False
70+
"""
71+
:type board: List[List[str]]
72+
:type word: str
73+
:rtype: bool
74+
"""
75+

Blind 75/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1>Blind 75</h1>
22
<a href="https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions">Leetcode link to the post.</a>
3-
<h4>Total Completed: 28</h4>
3+
<h4>Total Completed: 29</h4>
44
<h2>Array</h2>
55
<ul>
66
<li><a href="Programs/Two Sum.py">Two Sum</a>
@@ -213,7 +213,11 @@
213213
Then flip the matrix upside down.
214214
</p>
215215
</li>
216-
<li>Word Search</li>
216+
<li><a href="Programs/Word Search.py">Word Search</a>
217+
<p><b>Approach</b>: Similar to island problem, dfs and backtracking for visited cells. <br/>
218+
Make sure to check if all the letters in the word are present on the board before proceeding to find the word.
219+
</p>
220+
</li>
217221
</ul>
218222
<h2>String</h2>
219223
<ul>

0 commit comments

Comments
 (0)