Skip to content

My solution #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
PLEASE FILL IN THE FOLLOWING!

## Full Name
John Johnson
Derek Thibert

## UWindsor Email
john.johnson@uwindsor.ca
thibertd@uwindsor.ca

## Application Form
- [ ] I certify I have submitted the [application form](https://forms.office.com/r/R4A1JyB3Xf)
- [x] I certify I have submitted the [application form](https://forms.office.com/r/R4A1JyB3Xf)

## Briefly explain how your solution works and how to run it

My solution...
My solution uses a backtracking solution which is pretty much brute force which goes through every cell to find a letter that matches with the word. The solution starts with the first letter of the word (if found) and then scopes the neighbours for a match without using the same letter cell.

To run it: make sure Python is installed and extensions are installed in Visual Studio Code. Click on the triangle in the upper right hand corner in Visual Studio Code and click on "Run Python File".

You can also type "py solution.py" to run it.
35 changes: 35 additions & 0 deletions solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import List

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
path = set()

def dfs(r, c, i):
if i == len(word):
return True
if (r < 0 or c < 0 or
r >= ROWS or c >= COLS or
word[i] != board[r][c] or
(r, c) in path):
return False

path.add((r, c))
res = (dfs(r + 1, c, i + 1) or
dfs(r - 1, c, i + 1) or
dfs(r, c + 1, i + 1) or
dfs(r, c - 1, i + 1))
path.remove((r, c))
return res

for r in range(ROWS):
for c in range(COLS):
if dfs(r, c, 0): return True
return False

ob1 = Solution()
print(ob1.exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"SEE"))
ob2 = Solution()
print(ob2.exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"ABCCED"))
ob3 = Solution()
print(ob3.exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"ABCB"))