Skip to content

Team A Solution #20

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
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
Mathew Pellarin

## UWindsor Email
john.johnson@uwindsor.ca
pellarim@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 first checks for any cases that might cause the program to timeout due to run time length. It first checks if there is enough occurences of the letters within the board for the given the string and if not fails it. It then check if the last character appears more times then the first and reverses the string if so.

A recursive function is called and this function first goes through prechecks to check if a letter is matched, the rows and coloumns are in range, if the letter was visted before or if the letter does not match the needed letter. If the letter is correct, add it to a correct set and check all adjecent letters for the next character calling itself recursivly. Once all letters are found return the restult

The variables are hard coded for testing. To test other inputs, chnaging the board and word variable will allow for other testing results. There is also a folder with a script that can be run on codeleet
58 changes: 0 additions & 58 deletions README.md

This file was deleted.

65 changes: 65 additions & 0 deletions solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from typing import List

def exist(board: List[List[str]], word: str) -> bool:
# get all lengths of input
rows, cols, word_len = len(board), len(board[0]), len(word)

# convert board to a string
board_str = ''.join([''.join(row) for row in board])

# check if there are enough occurences of each character in the word in the board
for char in word:
if word.count(char) > board_str.count(char):
return False

# check if the last char appears less times then the first char, reverse the string if so
if board_str.count(word[0]) < board_str.count(word[-1]):
word = word[::-1]

# create a visited set
seen = set()

# row and col are the current position of the board, n is the current position of the word
def check(row, col, n=0):
# check if the word exists
if n == word_len:
return True

# check if the row and col are in range
if row < 0 or row >= rows or col < 0 or col >= cols:
return False

# check if the current position is visited
if (row, col) in seen:
return False

# backtrack if the current position of the board is not equal to the current position of the word
if board[row][col] != word[n]:
return False

# mark the letter as visited
seen.add((row, col))

# check if the word is found in any direction
found = check(row + 1, col, n + 1) or \
check(row - 1, col, n + 1) or \
check(row, col + 1, n + 1) or \
check(row, col - 1, n + 1)

# mark the letter as unvisited
seen.remove((row, col))

# return the result
return found

# check if the word exists in the board
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']]
word = "ABCCED"

print(exist(board, word))