Skip to content

my solution #23

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 1 commit 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
10 changes: 6 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
PLEASE FILL IN THE FOLLOWING!

## Full Name
John Johnson
Abbie Dewhirst

## UWindsor Email
john.johnson@uwindsor.ca
dyck41@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...
It runs, gg.....

It calls itself recursively to go through the board to see if the word is in it.
50 changes: 50 additions & 0 deletions solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import List

def inBoard(board: List[List[str]], givenWord: str) -> bool:
rows, cols, length = len(board), len(board[0]), len(givenWord)

board_str = ''.join([''.join(row) for row in board])

for char in givenWord:
if givenWord.count(char) > board_str.count(char):
return False

if board_str.count(givenWord[0]) < board_str.count(givenWord[-1]):
givenWord = givenWord[::-1]

seen = set()

def check(row, col, i=0):
if i == length:
return True

if row < 0 or row >= rows or col < 0 or col >= cols:
return False

if (row, col) in seen:
return False

if board[row][col] != givenWord[i]:
return False

seen.add((row, col))

found = check(row + 1, col, i + 1) or \
check(row - 1, col, i + 1) or \
check(row, col + 1, i + 1) or \
check(row, col - 1, i + 1)

seen.remove((row, col))

return found

return any(check(row, col) for row in range(rows) for col in range(cols))

if __name__ == '__main__':
board = [
['A', 'B', 'C', 'E'],
['S', 'F', 'C', 'S'],
['A', 'D', 'E', 'E']]
givenWord = "ABCCED"

print(inBoard(board, givenWord))