Program to check words can be found in matrix character board or not in Python

Suppose we have a matrix character board where each cell holds a character. We also have a string called target, and we need to check whether the target can be found in the matrix by going left-to-right (horizontally) or top-to-bottom (vertically) in a unidirectional way.

Problem Example

If the input matrix is ?

a n t s
s p i n
l a p s

And word = "tip", then the output will be True because the third column (top to bottom) forms "tip".

Algorithm

To solve this problem, we will follow these steps ?

  • Check all rows: for each row in the board, join characters to form a string and check if the target word exists
  • Check all columns: for each column in the board, join characters from top to bottom and check if the target word exists
  • Return True if word is found in any row or column, otherwise return False

Implementation

def solve(board, word):
    # Check rows (left to right)
    for row in board:
        row_string = "".join(row)
        if word in row_string:
            return True
    
    # Check columns (top to bottom)  
    for col_index in range(len(board[0])):
        col_string = "".join([row[col_index] for row in board])
        if word in col_string:
            return True
    
    return False

# Test the function
board = [["a","n","t","s"],["s","p","i","n"],["l","a","p","s"]]
word = "tip"
print(solve(board, word))
True

How It Works

The algorithm works in two phases ?

  1. Row checking: Convert each row to a string and check if the target word exists as a substring
  2. Column checking: Extract each column by taking characters at the same index from all rows, then check if the target word exists

Additional Example

Let's test with a word that doesn't exist ?

def solve(board, word):
    # Check rows (left to right)
    for row in board:
        row_string = "".join(row)
        if word in row_string:
            return True
    
    # Check columns (top to bottom)  
    for col_index in range(len(board[0])):
        col_string = "".join([row[col_index] for row in board])
        if word in col_string:
            return True
    
    return False

# Test with different words
board = [["a","n","t","s"],["s","p","i","n"],["l","a","p","s"]]

# Test existing words
print("'tip' found:", solve(board, "tip"))    # Column word
print("'spin' found:", solve(board, "spin"))  # Row word  
print("'xyz' found:", solve(board, "xyz"))    # Non-existing word
'tip' found: True
'spin' found: True
'xyz' found: False

Time and Space Complexity

  • Time Complexity: O(m × n) where m is the number of rows and n is the number of columns
  • Space Complexity: O(n) for storing the column strings

Conclusion

This approach efficiently checks for word existence in a matrix by examining both horizontal rows and vertical columns. The solution handles unidirectional word searching in O(m × n) time complexity.

Updated on: 2026-03-26T16:42:58+05:30

617 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements