-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Add backtrack word search in matrix #8005
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
Merged
cclauss
merged 14 commits into
TheAlgorithms:master
from
alexpantyukhin:add_backtrack_word_search_in_matrix
Nov 29, 2022
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7972862
add backtracking word search
alexpantyukhin b05215a
updating DIRECTORY.md
259300f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7430653
review notes fixes
alexpantyukhin 77b0e16
additional fixes
alexpantyukhin 1ba52fb
add tests
alexpantyukhin cb2c632
small cleanup
alexpantyukhin 8697533
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7e98074
small cleanup 2
alexpantyukhin d50711a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4e65726
Update backtracking/word_search.py
alexpantyukhin 4e9133b
Update backtracking/word_search.py
alexpantyukhin 7f765d0
Update backtracking/word_search.py
alexpantyukhin 295dcd1
Update backtracking/word_search.py
alexpantyukhin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
""" | ||
Author : Alexander Pantyukhin | ||
Date : November 24, 2022 | ||
|
||
Task: | ||
Given an m x n grid of characters board and a string word, | ||
return true if word exists in the grid. | ||
|
||
The word can be constructed from letters of sequentially adjacent cells, | ||
where adjacent cells are horizontally or vertically neighboring. | ||
The same letter cell may not be used more than once. | ||
|
||
Example: | ||
|
||
Matrix: | ||
--------- | ||
|A|B|C|E| | ||
|S|F|C|S| | ||
|A|D|E|E| | ||
--------- | ||
|
||
Word: | ||
"ABCCED" | ||
|
||
Result: | ||
True | ||
|
||
Implementation notes: Use backtracking approach. | ||
At each point, check all neighbors to try to find the next letter of the word. | ||
|
||
leetcode: https://leetcode.com/problems/word-search/ | ||
|
||
""" | ||
|
||
|
||
def word_exists(board: list[list[str]], word: str) -> bool: | ||
""" | ||
>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED") | ||
True | ||
>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE") | ||
True | ||
>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB") | ||
False | ||
>>> word_exists([["A"]], "A") | ||
True | ||
>>> word_exists([["A","A","A","A","A","A"], | ||
... ["A","A","A","A","A","A"], | ||
... ["A","A","A","A","A","A"], | ||
... ["A","A","A","A","A","A"], | ||
... ["A","A","A","A","A","B"], | ||
... ["A","A","A","A","B","A"]], | ||
... "AAAAAAAAAAAAABB") | ||
False | ||
>>> word_exists([["A"]], 123) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The word parameter should be a string of length greater than 0. | ||
>>> word_exists([["A"]], "") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The word parameter should be a string of length greater than 0. | ||
>>> word_exists([[]], "AB") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The board should be a non empty matrix of single chars strings. | ||
>>> word_exists([], "AB") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The board should be a non empty matrix of single chars strings. | ||
>>> word_exists([["A"], [21]], "AB") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The board should be a non empty matrix of single chars strings. | ||
""" | ||
|
||
# Validate board | ||
board_error_message = ( | ||
"The board should be a non empty matrix of single chars strings." | ||
) | ||
if not isinstance(board, list) or len(board) == 0: | ||
raise ValueError(board_error_message) | ||
|
||
for row in board: | ||
if not isinstance(row, list) or len(row) == 0: | ||
raise ValueError(board_error_message) | ||
|
||
for item in row: | ||
if not isinstance(item, str) or len(item) != 1: | ||
raise ValueError(board_error_message) | ||
|
||
# Validate word | ||
if not isinstance(word, str) or len(word) == 0: | ||
raise ValueError( | ||
"The word parameter should be a string of length greater than 0." | ||
) | ||
|
||
traverts_directions = [(0, 1), (0, -1), (-1, 0), (1, 0)] | ||
len_word = len(word) | ||
len_board = len(board) | ||
len_board_column = len(board[0]) | ||
|
||
# Returns the hash key of matrix indexes. | ||
def get_point_key(row: int, column: int) -> int: | ||
""" | ||
>>> len_board=10 | ||
>>> len_board_column=20 | ||
>>> get_point_key(0, 0) | ||
200 | ||
""" | ||
|
||
return len_board * len_board_column * row + column | ||
|
||
# Return True if it's possible to search the word suffix | ||
# starting from the word_index. | ||
def exits_word( | ||
alexpantyukhin marked this conversation as resolved.
Show resolved
Hide resolved
alexpantyukhin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
row: int, column: int, word_index: int, visited_points_set: set[int] | ||
) -> bool: | ||
""" | ||
>>> board=[["A"]] | ||
>>> word="B" | ||
>>> exits_word(0, 0, 0, set()) | ||
False | ||
""" | ||
|
||
if board[row][column] != word[word_index]: | ||
return False | ||
|
||
if word_index == len_word - 1: | ||
return True | ||
|
||
for direction in traverts_directions: | ||
next_i = row + direction[0] | ||
next_j = column + direction[1] | ||
if not (0 <= next_i < len_board and 0 <= next_j < len_board_column): | ||
continue | ||
|
||
key = get_point_key(next_i, next_j) | ||
if key in visited_points_set: | ||
continue | ||
|
||
visited_points_set.add(key) | ||
if exits_word(next_i, next_j, word_index + 1, visited_points_set): | ||
return True | ||
|
||
visited_points_set.remove(key) | ||
|
||
return False | ||
|
||
for i in range(len_board): | ||
for j in range(len_board_column): | ||
if exits_word(i, j, 0, {get_point_key(i, j)}): | ||
return True | ||
|
||
return False | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.