backtracking.word_search¶

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/

Functions¶

exits_word(→ bool)

Return True if it's possible to search the word suffix

get_point_key(→ int)

Returns the hash key of matrix indexes.

word_exists(→ bool)

Module Contents¶

backtracking.word_search.exits_word(board: list[list[str]], word: str, row: int, column: int, word_index: int, visited_points_set: set[int]) → bool¶

Return True if it’s possible to search the word suffix starting from the word_index.

>>> exits_word([["A"]], "B", 0, 0, 0, set())
False
backtracking.word_search.get_point_key(len_board: int, len_board_column: int, row: int, column: int) → int¶

Returns the hash key of matrix indexes.

>>> get_point_key(10, 20, 1, 0)
200
backtracking.word_search.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([["B", "A", "A"], ["A", "A", "A"], ["A", "B", "A"]], "ABB")
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.

thealgorithms-python

Navigation

index.md

  • Contributing guidelines
  • Getting Started
  • Community Channels
  • List of Algorithms
  • MIT License
  • API Reference
    • maths
    • other
    • sorts
    • graphs
    • hashes
    • matrix
    • ciphers
    • geodesy
    • physics
    • quantum
    • strings
    • fractals
    • geometry
    • graphics
    • knapsack
    • searches
    • financial
    • blockchain
    • scheduling
    • conversions
    • electronics
    • fuzzy_logic
    • backtracking
    • audio_filters
    • file_transfer
    • project_euler
    • greedy_methods
    • linear_algebra
    • neural_network
    • boolean_algebra
    • computer_vision
    • data_structures
    • networking_flow
    • web_programming
    • bit_manipulation
    • data_compression
    • machine_learning
    • cellular_automata
    • genetic_algorithm
    • divide_and_conquer
    • linear_programming
    • dynamic_programming
    • digital_image_processing

Related Topics

  • Documentation overview
    • API Reference
      • backtracking
        • Previous: backtracking.word_ladder
        • Next: audio_filters
©2014, TheAlgorithms. | Powered by Sphinx 8.2.3 & Alabaster 1.0.0 | Page source