From 6f64304f4944b65d55396b2c2e94e290cba33501 Mon Sep 17 00:00:00 2001 From: Derek Thibert <56123156+thibert-derek@users.noreply.github.com> Date: Fri, 2 Dec 2022 11:47:19 -0500 Subject: [PATCH 1/3] Add files via upload --- solution.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 solution.py diff --git a/solution.py b/solution.py new file mode 100644 index 0000000..abfbd12 --- /dev/null +++ b/solution.py @@ -0,0 +1,26 @@ +class Solution(object): + def exist(self, board, word): + n = len(board) + m = len(board[0]) + for i in range(n): + for j in range(m): + if word[0] == board[i][j]: + if self.find(board,word,i,j): + return True + return False + def find(self, board,word,row,col,i=0): + if i== len(word): + return True + if row>= len(board) or row <0 or col >=len(board[0]) or col<0 or word[i]!=board[row][col]: + return False + board[row][col] = '*' + res = self.find(board,word,row+1,col,i+1) or self.find(board,word,row-1,col,i+1) or self.find(board,word,row,col+1,i+1) or self.find(board,word,row,col-1,i+1) + board[row][col] = word[i] + return res + +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")) \ No newline at end of file From 1963522a36b2867fbfc25a429ecd8797be823df9 Mon Sep 17 00:00:00 2001 From: Derek Thibert <56123156+thibert-derek@users.noreply.github.com> Date: Fri, 2 Dec 2022 20:19:27 -0500 Subject: [PATCH 2/3] Update solution.py --- solution.py | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/solution.py b/solution.py index abfbd12..b6cc521 100644 --- a/solution.py +++ b/solution.py @@ -1,26 +1,35 @@ -class Solution(object): - def exist(self, board, word): - n = len(board) - m = len(board[0]) - for i in range(n): - for j in range(m): - if word[0] == board[i][j]: - if self.find(board,word,i,j): - return True - return False - def find(self, board,word,row,col,i=0): - if i== len(word): - return True - if row>= len(board) or row <0 or col >=len(board[0]) or col<0 or word[i]!=board[row][col]: - return False - board[row][col] = '*' - res = self.find(board,word,row+1,col,i+1) or self.find(board,word,row-1,col,i+1) or self.find(board,word,row,col+1,i+1) or self.find(board,word,row,col-1,i+1) - board[row][col] = word[i] - return res - +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")) \ No newline at end of file +print(ob3.exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"ABCB")) From 5457931bf0c25f677d3355498c1c4be5fadcf953 Mon Sep 17 00:00:00 2001 From: Derek Thibert <56123156+thibert-derek@users.noreply.github.com> Date: Fri, 2 Dec 2022 20:48:32 -0500 Subject: [PATCH 3/3] Update pull_request_template.md --- .github/pull_request_template.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 9ae4f93..c360c3d 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -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.