
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 is holding a character. We also have a string called target, we have to check whether the target can be found in the matrix by going left-to-right, or up-to-down unidirectional way, or not.
So, if the input is like
a | n | t | s |
s | p | i | n |
l | a | p | s |
Word = “tip”
then the output will be True, you can see the third column (top to bottom) is forming "tip".
To solve this, we will follow these steps −
- for each i in board, do
- i := make word from characters present in i
- if word is present in i, then
- return True
- i := 0
- while i < row count of board, do
- j := make string from the characters of ith column in board
- i := i + 1
- if word is in j, then
- return True
- return False
Example
Let us see the following implementation to get better understanding −
def solve(board, word): for i in board: i = "".join(i) if word in i: return True i = 0 while i < len(board): j = "".join([col[i] for col in board]) i += 1 if word in j: return True return False board = [["a","n","t","s"],["s","p","i","n"],["l","a","p","s"]] word = "tip" print(solve(board, word))
Input
[["a","n","t","s"], ["s","p","i","n"], ["l","a","p","s"]], "tip"
Output
True
- Related Articles
- Program to check a string can be broken into given list of words or not in python
- Program to check whether given matrix is Toeplitz Matrix or not in Python
- Program to check whether a board is valid N queens solution or not in python
- Program to check subarrays can be rearranged from arithmetic sequence or not in Python
- Check if a two-character string can be made using given words in Python
- Program to check whether one point can be converted to another or not in Python
- Program to check n can be shown as sum of k or not in Python
- Program to check two strings can be equal by swapping characters or not in Python
- Program to check a string can be split into three palindromes or not in Python
- Program to check we can spell out the target by a list of words or not in Python
- Program to check some elements in matrix forms a cycle or not in python
- Program to check whether two trees can be formed by swapping nodes or not in Python
- Program to check all tasks can be executed using given server cores or not in Python
- Program to check n can be represented as sum of k primes or not in Python
- Program to check if a matrix is Binary matrix or not in C++

Advertisements