Thursday, December 26, 2024
Google search engine
HomeData Modelling & AISearch a Word in a 2D Grid of characters (Word Search)

Search a Word in a 2D Grid of characters (Word Search)

Given a 2D grid of characters and a single word/an array of words, find all occurrences of the given word/words in the grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form).
The 8 directions are, Horizontally Left, Horizontally Right, Vertically Up, Vertically Down and 4 Diagonal directions.

Example: 

Input:  grid[][] = {"GEEKSFORGEEKS",
"GEEKSQUIZGEEK",
"IDEQAPRACTICE"};
word = "GEEKS"

Output: pattern found at 0, 0
pattern found at 0, 8
pattern found at 1, 0
Explanation: 'GEEKS' can be found as prefix of
1st 2 rows and suffix of first row

Input: grid[][] = {"GEEKSFORGEEKS",
"GEEKSQUIZGEEK",
"IDEQAPRACTICE"};
word = "EEE"

Output: pattern found at 0, 2
pattern found at 0, 10
pattern found at 2, 2
pattern found at 2, 12
Explanation: EEE can be found in first row
twice at index 2 and index 10
and in second row at 2 and 12

Below diagram shows a bigger grid and presence of different words in it. 

wordsearch(1)

Source: Microsoft Interview Question.
 

Recommended Practice

Approach when a single word is given: The idea used here is simple, we check every cell. If cell has first character, then we one by one try all 8 directions from that cell for a match. Implementation is interesting though. We use two arrays x[] and y[] to find next move in all 8 directions. 
Below are implementation of the same:  

C++




// C++ programs to search a word in a 2D grid
#include <bits/stdc++.h>
using namespace std;
 
// For searching in all 8 direction
int x[] = { -1, -1, -1,  0, 0,  1, 1, 1 };
int y[] = { -1,  0,  1, -1, 1, -1, 0, 1 };
 
// This function searches in
// all 8-direction from point
// (row, col) in grid[][]
bool search2D(char *grid, int row, int col,
               string word, int R, int C)
{
    // If first character of word doesn't
    // match with given starting point in grid.
    if (*(grid+row*C+col) != word[0])
        return false;
 
    int len = word.length();
 
    // Search word in all 8 directions
    // starting from (row, col)
    for (int dir = 0; dir < 8; dir++) {
        // Initialize starting point
        // for current direction
        int k, rd = row + x[dir], cd = col + y[dir];
 
        // First character is already checked,
        // match remaining characters
        for (k = 1; k < len; k++) {
            // If out of bound break
            if (rd >= R || rd < 0 || cd >= C || cd < 0)
                break;
 
            // If not matched,  break
            if (*(grid+rd*C+cd) != word[k])
                break;
 
            // Moving in particular direction
            rd += x[dir], cd += y[dir];
        }
 
        // If all character matched, then value of k must
        // be equal to length of word
        if (k == len)
            return true;
    }
    return false;
}
 
// Searches given word in a given
// matrix in all 8 directions
void patternSearch(char *grid, string word,
                  int R, int C)
{
    // Consider every point as starting
    // point and search given word
    for (int row = 0; row < R; row++)
        for (int col = 0; col < C; col++)
            if (search2D(grid, row, col, word, R, C))
                cout << "pattern found at "
                     << row << ", "
                     << col << endl;
}
 
// Driver program
int main()
{
      int R = 3, C = 13;
    char grid[R][C] = { "GEEKSFORGEEKS",
                        "GEEKSQUIZGEEK",
                        "IDEQAPRACTICE" };
 
    patternSearch((char *)grid, "GEEKS", R, C);
    cout << endl;
    patternSearch((char *)grid, "EEE", R, C);
    return 0;
}


Java




// Java program to search
// a word in a 2D grid
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Rows and columns in the given grid
    static int R, C;
 
    // For searching in all 8 direction
    static int[] x = { -1, -1, -1, 0, 0, 1, 1, 1 };
    static int[] y = { -1, 0, 1, -1, 1, -1, 0, 1 };
 
    // This function searches in all
    // 8-direction from point
    // (row, col) in grid[][]
    static boolean search2D(char[][] grid, int row,
                            int col, String word)
    {
        // If first character of word
        // doesn't match with
        // given starting point in grid.
        if (grid[row][col] != word.charAt(0))
            return false;
 
        int len = word.length();
 
        // Search word in all 8 directions
        // starting from (row, col)
        for (int dir = 0; dir < 8; dir++) {
            // Initialize starting point
            // for current direction
            int k, rd = row + x[dir], cd = col + y[dir];
 
            // First character is already checked,
            // match remaining characters
            for (k = 1; k < len; k++) {
                // If out of bound break
                if (rd >= R || rd < 0 || cd >= C || cd < 0)
                    break;
 
                // If not matched, break
                if (grid[rd][cd] != word.charAt(k))
                    break;
 
                // Moving in particular direction
                rd += x[dir];
                cd += y[dir];
            }
 
            // If all character matched,
            // then value of must
            // be equal to length of word
            if (k == len)
                return true;
        }
        return false;
    }
 
    // Searches given word in a given
    // matrix in all 8 directions
    static void patternSearch(
        char[][] grid,
        String word)
    {
        // Consider every point as starting
        // point and search given word
        for (int row = 0; row < R; row++) {
            for (int col = 0; col < C; col++) {
                if (grid[row][col]==word.charAt(0)  &&
                    search2D(grid, row, col, word))
                        System.out.println(
                            "pattern found at " + row + ", " + col);
            }
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        R = 3;
        C = 13;
        char[][] grid = { { 'G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S' },
                          { 'G', 'E', 'E', 'K', 'S', 'Q', 'U', 'I', 'Z', 'G', 'E', 'E', 'K' },
                          { 'I', 'D', 'E', 'Q', 'A', 'P', 'R', 'A', 'C', 'T', 'I', 'C', 'E' } };
        patternSearch(grid, "GEEKS");
        System.out.println();
        patternSearch(grid, "EEE");
    }
}
 
// This code is contributed by rachana soma


Python3




# Python3 program to search a word in a 2D grid
class GFG:
     
    def __init__(self):
        self.R = None
        self.C = None
        self.dir = [[-1, 0], [1, 0], [1, 1],
                    [1, -1], [-1, -1], [-1, 1],
                    [0, 1], [0, -1]]
                     
    # This function searches in all 8-direction
    # from point(row, col) in grid[][]
    def search2D(self, grid, row, col, word):
         
        # If first character of word doesn't match
        # with the given starting point in grid.
        if grid[row][col] != word[0]:
            return False
             
        # Search word in all 8 directions
        # starting from (row, col)
        for x, y in self.dir:
             
            # Initialize starting point
            # for current direction
            rd, cd = row + x, col + y
            flag = True
             
            # First character is already checked,
            # match remaining characters
            for k in range(1, len(word)):
                 
                # If out of bound or not matched, break
                if (0 <= rd <self.R and
                    0 <= cd < self.C and
                    word[k] == grid[rd][cd]):
                     
                    # Moving in particular direction
                    rd += x
                    cd += y
                else:
                    flag = False
                    break
             
            # If all character matched, then
            # value of flag must be false       
            if flag:
                return True
        return False
         
    # Searches given word in a given matrix
    # in all 8 directions   
    def patternSearch(self, grid, word):
         
        # Rows and columns in given grid
        self.R = len(grid)
        self.C = len(grid[0])
         
        # Consider every point as starting point
        # and search given word
        for row in range(self.R):
            for col in range(self.C):
                if self.search2D(grid, row, col, word):
                    print("pattern found at " +
                           str(row) + ', ' + str(col))
                     
# Driver Code
if __name__=='__main__':
    grid = ["GEEKSFORGEEKS",
            "GEEKSQUIZGEEK",
            "IDEQAPRACTICE"]
    gfg = GFG()
    gfg.patternSearch(grid, 'GEEKS')
    print('')
    gfg.patternSearch(grid, 'EEE')
     
# This code is contributed by Yezheng Li


C#




// C# program to search a word in a 2D grid
using System;
class GFG {
 
    // Rows and columns in given grid
    static int R, C;
 
    // For searching in all 8 direction
    static int[] x = { -1, -1, -1, 0, 0, 1, 1, 1 };
    static int[] y = { -1, 0, 1, -1, 1, -1, 0, 1 };
 
    // This function searches in all 8-direction
    // from point (row, col) in grid[, ]
    static bool search2D(char[, ] grid, int row,
                         int col, String word)
    {
        // If first character of word doesn't match
        // with given starting point in grid.
        if (grid[row, col] != word[0]) {
            return false;
        }
 
        int len = word.Length;
 
        // Search word in all 8 directions
        // starting from (row, col)
        for (int dir = 0; dir < 8; dir++) {
            // Initialize starting point
            // for current direction
            int k, rd = row + x[dir], cd = col + y[dir];
 
            // First character is already checked,
            // match remaining characters
            for (k = 1; k < len; k++) {
                // If out of bound break
                if (rd >= R || rd < 0 || cd >= C || cd < 0) {
                    break;
                }
 
                // If not matched, break
                if (grid[rd, cd] != word[k]) {
                    break;
                }
 
                // Moving in particular direction
                rd += x[dir];
                cd += y[dir];
            }
 
            // If all character matched, then value of k
            // must be equal to length of word
            if (k == len) {
                return true;
            }
        }
        return false;
    }
 
    // Searches given word in a given
    // matrix in all 8 directions
    static void patternSearch(char[, ] grid,
                              String word)
    {
        // Consider every point as starting
        // point and search given word
        for (int row = 0; row < R; row++) {
            for (int col = 0; col < C; col++) {
                if (search2D(grid, row, col, word)) {
                    Console.WriteLine("pattern found at " + row + ", " + col);
                }
            }
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        R = 3;
        C = 13;
        char[, ] grid = { { 'G', 'E', 'E', 'K', 'S', 'F', 'O',
                            'R', 'G', 'E', 'E', 'K', 'S' },
                          { 'G', 'E', 'E', 'K', 'S', 'Q', 'U',
                            'I', 'Z', 'G', 'E', 'E', 'K' },
                          { 'I', 'D', 'E', 'Q', 'A', 'P', 'R',
                            'A', 'C', 'T', 'I', 'C', 'E' } };
        patternSearch(grid, "GEEKS");
        Console.WriteLine();
        patternSearch(grid, "EEE");
    }
}
 
#This code is contributed by Rajput - Ji


Javascript




<script>
 
// JavaScript program to search
// a word in a 2D grid
 
 // Rows and columns in the given grid
let R, C;
 
// For searching in all 8 direction
let x=[-1, -1, -1, 0, 0, 1, 1, 1];
 
let y=[-1, 0, 1, -1, 1, -1, 0, 1];
 
// This function searches in all
    // 8-direction from point
    // (row, col) in grid[][]
function search2D(grid,row,col,word)
{
    // If first character of word
        // doesn't match with
        // given starting point in grid.
        if (grid[row][col] != word[0])
            return false;
  
        let len = word.length;
  
        // Search word in all 8 directions
        // starting from (row, col)
        for (let dir = 0; dir < 8; dir++) {
            // Initialize starting point
            // for current direction
            let k, rd = row + x[dir], cd = col + y[dir];
  
            // First character is already checked,
            // match remaining characters
            for (k = 1; k < len; k++) {
                // If out of bound break
                if (rd >= R || rd < 0 || cd >= C || cd < 0)
                    break;
  
                // If not matched, break
                if (grid[rd][cd] != word[k])
                    break;
  
                // Moving in particular direction
                rd += x[dir];
                cd += y[dir];
            }
  
            // If all character matched,
            // then value of must
            // be equal to length of word
            if (k == len)
                return true;
        }
        return false;
}
 
// Searches given word in a given
    // matrix in all 8 directions
function patternSearch( grid,word)
{
    // Consider every point as starting
        // point and search given word
        for (let row = 0; row < R; row++) {
            for (let col = 0; col < C; col++) {
                if (search2D(grid, row, col, word))
                    document.write(
                        "pattern found at " + row + ", " + col+"<br>");
            }
        }
}
 
// Driver code
R = 3;
C = 13;
let grid = [[ 'G', 'E', 'E', 'K', 'S', 'F', 'O',
'R', 'G', 'E', 'E', 'K', 'S' ],
 
[ 'G', 'E', 'E', 'K', 'S', 'Q', 'U', 'I', 'Z',
'G', 'E', 'E', 'K' ],
 
[ 'I', 'D', 'E', 'Q', 'A', 'P', 'R', 'A', 'C',
'T', 'I', 'C', 'E' ] ];
patternSearch(grid, "GEEKS");
document.write("<br>");
patternSearch(grid, "EEE");
 
 
// This code is contributed by avanitrachhadiya2155
</script>


Output

pattern found at 0, 0
pattern found at 0, 8
pattern found at 1, 0

pattern found at 0, 2
pattern found at 0, 10
pattern found at 2, 2
pattern found at 2, 12

Complexity Analysis:  

  • Time complexity: O(R*C*8*len(str)). 
    All the cells will be visited and traversed in all 8 directions, where R and C is side of matrix so time complexity is O(R*C).
  • Auxiliary Space: O(1). 
    As no extra space is needed.

Approach when array of words is given: The idea used here is simple, we check every cell. If cell has first character, then we one by one try all 8 directions from that cell for a match, put this check in a loop.  We use mover array to store the manner in which next moves are possible={left,right,up,down,…diagonals}. 
Below are implementation of the same:  

C++




#include <bits/stdc++.h>
using namespace std;
 
// making a solution class to solve the problem and to keep
// the components and functions of solution together
class Solution {
public:
    // making the possible moves in movers array
    vector<vector<int> > mover
        = { { 1, 0 }, { 0, 1 },   { -1, 0 }, { 0, -1 },
            { 1, 1 }, { -1, -1 }, { 1, -1 }, { -1, 1 } };
    // making the board global variable
    vector<vector<char> > board;
    // depth first search for the string, with the
    // coordinates and a visited array to take care that we
    // do not overlap the places visited already
    bool dfs(int x, int y, string& s,
             vector<vector<bool> > vis)
    {
        // if string length becomes 0 means string is found
        if (s.length() == 0)
            return true;
        vis[x][y] = true;
        // making a solution boolean to see if we can
        // perform depth search to find answer
        bool sol = false;
        // making possible moves
        for (int i = 0; i < mover.size(); i++) {
            int curr_x = mover[i][0] + x;
            int curr_y = mover[i][1] + y;
            // checking for out of bound areas
            if (curr_x >= 0 && curr_x < board.size()) {
                if (curr_y >= 0
                    && curr_y < board[0].size()) {
                    // checking for similarity in the first
                    // letter and the visited array
                    if (board[curr_x][curr_y] == s[0]
                        && vis[curr_x][curr_y] == false) {
                        string k = s.substr(
                            1); // removing the first letter
                                // from the string
                        sol |= dfs(curr_x, curr_y, k, vis);
                    }
                }
            }
        }
        return sol;
    }
    // making a function findwords to find words along with
    // their location which inputs the board and list of
    // words
    vector<string> findWords(vector<vector<char> >& board,
                             vector<string>& words)
    {
        this->board
            = board; // making board a global variable
        vector<string> ans;
        vector<vector<bool> > vis(
            board.size(),
            vector<bool>(board[0].size(),
                         false)); // visited array
        for (auto& word : words) {
            for (int i = 0; i < board.size(); i++) {
                for (int j = 0; j < board[i].size(); j++) {
                    if (board[i][j] == word[0]) {
                        // if first letter of(i,j)==
                        // string's first letter then we can
                        // perform dfs to check the
                        // possibility of string being present
                        // from location (i,j)
                        string s = word.substr(1);
                        if (dfs(i, j, s, vis)) {
                            ans.push_back(
                                word + "->{" + to_string(i)
                                + "," + to_string(j) + "}");
                        }
                    }
                }
                if (ans.size() && ans.back() == word)
                    break;
            }
        }
        return ans;
    }
};
int main()
{
    // making 1 instance of class solution as solver
    Solution solver;
    vector<vector<char> > board
        = { { 'o', 'a', 'a', 'n' },
            { 'e', 't', 'a', 'e' },
            { 'i', 'h', 'k', 'r' },
            { 'i', 'f', 'l', 'v' } };
    vector<string> words = { "oath", "pea", "eat", "rain" };
    // using the function findwords from our solution class
    // to find the answer
    vector<string> ans = solver.findWords(board, words);
    // printing the answer
    for (auto& part : ans)
        cout << part << endl;
    return 0;
}//contributed by NamanAnand


Java




import java.util.ArrayList;
import java.util.List;
 
// making a solution class to solve the problem and to keep
// the components and functions of solution together
class Solution {
 
    // making the possible moves in movers array
    private final List<List<Integer> > Mover = List.of(
        List.of(1, 0), List.of(0, 1), List.of(-1, 0),
        List.of(0, -1), List.of(1, 1), List.of(-1, -1),
        List.of(1, -1), List.of(-1, 1));
 
    // making the board global variable
    private List<List<Character> > Board;
    private int Rows;
    private int Cols;
 
    // making a function findwords to find words along with
    // their location which inputs the board and list of
    // words
    public List<String> findWords(char[][] board,
                                  String[] words)
    {
        var result = new ArrayList<String>();
        Rows = board.length;
        Cols = board[0].length;
 
        // making board a global variable
        Board = new ArrayList<>(Rows);
        for (int i = 0; i < Rows; i++) {
            Board.add(new ArrayList<Character>());
 
            for (int j = 0; j < Cols; j++) {
                Board.get(i).add(board[i][j]);
            }
        }
 
        for (String word : words) {
            for (int i = 0; i < Rows; i++) {
                for (int j = 0; j < Cols; j++) {
                    if (Board.get(i).get(j)
                        == word.charAt(0)) {
 
                        // making a function findwords to
                        // find words along with their
                        // location which inputs the board
                        // and list of words
                        if (dfs(i, j, word.substring(1),
                                new boolean[Rows][Cols])) {
                            result.add(word + "->{" + i
                                       + "," + j + "}");
                        }
                    }
                }
            }
        }
        return result;
    }
 
    // depth first search for the string, with the
    // coordinates and a visited array to take care that we
    // do not overlap the places visited already
    private boolean dfs(int x, int y, String s,
                        boolean[][] vis)
    {
 
        // if string length becomes 0 means string is found
        if (s.length() == 0) {
            return true;
        }
 
        vis[x][y] = true;
 
        // making a solution boolean to see if we can
        // perform depth search to find answer
        boolean sol = false;
 
        // making possible moves
        for (List<Integer> move : Mover) {
            int currX = move.get(0) + x;
            int currY = move.get(1) + y;
 
            // checking for out of bound areas
            if (currX >= 0 && currX < Rows && currY >= 0
                && currY < Cols) {
 
                // checking for similarity in the first
                // letter and the visited array
                if (Board.get(currX).get(currY)
                        == s.charAt(0)
                    && !vis[currX][currY]) {
                    if (dfs(currX, currY, s.substring(1),
                            vis)) {
 
                        // removing the first letter
                        // from the string
                        sol = true;
                    }
                }
            }
        }
        return sol;
    }
}
 
public class Main {
    public static void main(String[] args)
    {
 
        // making 1 instance of class solution as solver
        Solution solver = new Solution();
        char[][] board
            = new char[][] { { 'o', 'a', 'a', 'n' },
                             { 'e', 't', 'a', 'e' },
                             { 'i', 'h', 'k', 'r' },
                             { 'i', 'f', 'l', 'v' } };
        String[] words
            = new String[] { "oath", "pea", "eat", "rain" };
 
        // using the function findwords from our solution
        // class to find the answer
        List<String> ans = solver.findWords(board, words);
        for (String s : ans) {
            System.out.println(s);
        }
    }
}


Python3




from typing import List
 
 
class Solution:
    def __init__(self):
        # making the possible moves in movers array
        self.mover = [
            [1, 0], [0, 1], [-1, 0], [0, -1],
            [1, 1], [-1, -1], [1, -1], [-1, 1]
        ]
 
    # making a function findwords to find words along with
    # their location which inputs the board and list of
    # words
    def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
        result = []
        rows = len(board)
        cols = len(board[0])
 
        # making board a global variable
        self.board = board
 
        for word in words:
            for i in range(rows):
                for j in range(cols):
                    if self.board[i][j] == word[0]:
 
                        # making a function findwords to
                        # find words along with their
                        # location which inputs the board
                        # and list of words
                        if self.dfs(i, j, word[1:], [[False] * cols for _ in range(rows)]):
                            result.append(f"{word}->{{{i},{j}}}")
 
        return result
 
    # depth first search for the string, with the
    # coordinates and a visited array to take care that we
    # do not overlap the places visited already
    def dfs(self, x: int, y: int, s: str, vis: List[List[bool]]) -> bool:
 
        # if string length becomes 0 means string is found
        if not s:
            return True
 
        vis[x][y] = True
 
        # making a solution boolean to see if we can
        # perform depth search to find answer
        sol = False
 
        # making possible moves
        for move in self.mover:
            currX = move[0] + x
            currY = move[1] + y
 
            # checking for out of bound areas
            if 0 <= currX < len(self.board) and 0 <= currY < len(self.board[0]):
 
                # checking for similarity in the first
                # letter and the visited array
                if self.board[currX][currY] == s[0] and not vis[currX][currY]:
                    if self.dfs(currX, currY, s[1:], vis):
 
                        # removing the first letter
                        # from the string
                        sol = True
 
        return sol
 
 
# testing the code
solver = Solution()
board = [['o', 'a', 'a', 'n'], ['e', 't', 'a', 'e'],
         ['i', 'h', 'k', 'r'], ['i', 'f', 'l', 'v']]
words = ['oath', 'pea', 'eat', 'rain']
ans = solver.findWords(board, words)
for part in ans:
    print(part)


C#




using System;
using System.Collections.Generic;
 
// making a solution class to solve the problem and to keep
// the components and functions of solution together
class Solution {
 
  // making the possible moves in movers array
  private readonly List<List<int>> Mover = new List<List<int>>()
  {
    new List<int> { 1, 0 },
    new List<int> { 0, 1 },
    new List<int> { -1, 0 },
    new List<int> { 0, -1 },
    new List<int> { 1, 1 },
    new List<int> { -1, -1 },
    new List<int> { 1, -1 },
    new List<int> { -1, 1 }
  };
 
  // making the board global variable
  private List<List<char>> Board;
  private int Rows;
  private int Cols;
 
  // making a function findwords to find words along with
  // their location which inputs the board and list of
  // words
  public IList<string> FindWords(char[][] board, string[] words) {
    var result = new List<string>();
    Rows = board.Length;
    Cols = board[0].Length;
 
    // making board a global variable
    Board = new List<List<char>>(Rows);
    for (int i = 0; i < Rows; i++)
      Board.Add(new List<char>(board[i]));
 
    foreach (var word in words) {
      for (int i = 0; i < Rows; i++) {
        for (int j = 0; j < Cols; j++) {
          if (Board[i][j] == word[0]) {
 
            // making a function findwords to find words along with
            // their location which inputs the board and list of
            // words
            if (Dfs(i, j, word.Substring(1), new bool[Rows, Cols]))
              result.Add(word + "->{" + i + "," + j + "}");
          }
        }
      }
    }
    return result;
  }
 
  // depth first search for the string, with the
  // coordinates and a visited array to take care that we
  // do not overlap the places visited already
  private bool Dfs(int x, int y, string s, bool[,] vis) {
 
    // if string length becomes 0 means string is found
    if (s.Length == 0) return true;
 
    vis[x, y] = true;
 
    // making a solution boolean to see if we can
    // perform depth search to find answer
    bool sol = false;
 
    // making possible moves
    for (int i = 0; i < Mover.Count; i++) {
      int currX = Mover[i][0] + x;
      int currY = Mover[i][1] + y;
 
      // checking for out of bound areas
      if (currX >= 0 && currX < Rows && currY >= 0 && currY < Cols) {
 
        // checking for similarity in the first
        // letter and the visited array
        if (Board[currX][currY] == s[0] && !vis[currX, currY]) {
          if (Dfs(currX, currY, s.Substring(1), vis)) {
 
            // removing the first letter
            // from the string
            sol = true;
          }
        }
      }
    }
 
    return sol;
  }
}
 
class Program {
  static void Main(string[] args) {
 
    // making 1 instance of class solution as solver
    Solution solver = new Solution();
    char[][] board = new char[][] {
      new char[] { 'o', 'a', 'a', 'n' },
      new char[] { 'e', 't', 'a', 'e' },
      new char[] { 'i', 'h', 'k', 'r' },
      new char[] { 'i', 'f', 'l', 'v' }
    };
    string[] words = new string[] { "oath", "pea", "eat", "rain" };
 
    // using the function findwords from our solution class
    // to find the answer
    IList<string> ans = solver.FindWords(board, words);
    foreach (string s in ans) {
      Console.WriteLine(s);
    }
  }
}


Javascript




const MOVES = [
[1, 0],
[0, 1],
[-1, 0],
[0, -1],
[1, 1],
[-1, -1],
[1, -1],
[-1, 1]
];
 
class Solution {
constructor() {
this.board = [];
}
 
dfs(x, y, s, vis) {
if (s.length === 0) return true;
vis[x][y] = true;
let sol = false;
for (let i = 0; i < MOVES.length; i++) {
let curr_x = MOVES[i][0] + x;
let curr_y = MOVES[i][1] + y;
if (curr_x >= 0 && curr_x < this.board.length) {
if (curr_y >= 0 && curr_y < this.board[0].length) {
if (this.board[curr_x][curr_y] === s[0] && !vis[curr_x][curr_y]) {
let k = s.substring(1);
sol |= this.dfs(curr_x, curr_y, k, vis);
}
}
}
}
return sol;
}
 
findWords(board, words) {
this.board = board;
let ans = [];
let vis = Array(board.length).fill().map(() => Array(board[0].length).fill(false));
for (let word of words) {
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
if (board[i][j] === word[0]) {
let s = word.substring(1);
if (this.dfs(i, j, s, vis)) {
ans.push(${word}->[${i},${j}]);
}
}
}
if (ans.length && ans[ans.length - 1].startsWith(word)) {
break;
}
}
}
return ans;
}
}
 
let solver = new Solution();
let board = [
['o', 'a', 'a', 'n'],
['e', 't', 'a', 'e'],
['i', 'h', 'k', 'r'],
['i', 'f', 'l', 'v']
];
let words = ["oath", "pea", "eat", "rain"];
let ans = solver.findWords(board, words);
for (let part of ans) {
console.log(part);
}


Output

oath->{0,0}
eat->{1,0}
eat->{1,3}

Complexity Analysis:  

  • Time complexity: O(R*C*len(str)*Number(str)*len(str)). 
    All the cells will be visited and traversed in all 8 directions, where R and C is side of matrix so time complexity is O(R*C) for each string.
  • Auxiliary Space: O(R*C*Numberof(str)*len(str)). (due to visited array)

Another Approach:

The code implements a solution class that takes a board of characters and a list of words as input. It then performs a depth-first search to find if the words are present on the board. If a word is found, it is added to the output along with its position on the board. The code uses a visited array to keep track of the cells that have been visited during the search. Finally, the function returns a vector of strings, where each string is a word followed by its position on the board.

Implementation is given below:

C++




#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
class Solution {
private:
    vector<pair<int, int>> mover = {{1, 0}, {0, 1}, {-1, 0},
                                    {0, -1}, {1, 1}, {-1, -1},
                                    {1, -1}, {-1, 1}};
 
public:
    bool dfs(int x, int y, string word,
             vector<vector<bool>>& visited,
             vector<vector<char>>& board) {
       
        // If word length becomes 0, the string is found
        if (word.empty()) {
            return true;
        }
 
        visited[x][y] = true;
        bool sol = false;
 
        // Making possible moves
        for (auto move : mover) {
            int curr_x = move.first + x;
            int curr_y = move.second + y;
 
            // Checking for out of bound areas
            if (0 <= curr_x && curr_x < board.size() && 0 <= curr_y && curr_y < board[0].size()) {
                 
              // Checking for similarity in the first letter and the visited array
                if (board[curr_x][curr_y] == word[0] && !visited[curr_x][curr_y]) {
                    string s = word.substr(1);
                    sol |= dfs(curr_x, curr_y, s, visited, board);
                }
            }
        }
 
        visited[x][y] = false;
        return sol;
    }
 
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        vector<string> ans;
        vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));
 
        for (string word : words) {
            for (int i = 0; i < board.size(); i++) {
                for (int j = 0; j < board[0].size(); j++) {
                    if (board[i][j] == word[0]) {
                        string s = word.substr(1);
                        if (dfs(i, j, s, visited, board)) {
                            ans.push_back(word + " -> {" + to_string(i) + "," + to_string(j) + "}");
                        }
                    }
                }
            }
        }
 
        return ans;
    }
};
 
int main() {
    vector<vector<char>> board = {{'o', 'a', 'a', 'n'},
                                  {'e', 't', 'a', 'e'},
                                  {'i', 'h', 'k', 'r'},
                                  {'i', 'f', 'l', 'v'}};
    vector<string> words = {"oath", "pea", "eat", "rain"};
    Solution solver;
    vector<string> result = solver.findWords(board, words);
 
    for (string str : result) {
        cout << str << endl;
    }
 
    return 0;
}


Java




// Java Program for the above approach
 
import java.util.ArrayList;
import java.util.List;
 
class Solution {
 
    private int[][] mover = {{1, 0}, {0, 1}, {-1, 0},
                             {0, -1}, {1, 1}, {-1, -1},
                             {1, -1}, {-1, 1}};
 
    public boolean dfs(int x, int y, String word, boolean[][] visited,
                        char[][] board) {
 
        // If word length becomes 0, the string is found
        if (word.length() == 0) {
            return true;
        }
 
        visited[x][y] = true;
        boolean sol = false;
 
        // Making possible moves
        for (int[] move : mover) {
            int curr_x = move[0] + x;
            int curr_y = move[1] + y;
 
            // Checking for out of bound areas
            if (0 <= curr_x && curr_x < board.length && 0 <= curr_y && curr_y < board[0].length) {
 
                // Checking for similarity in the first letter and the visited array
                if (board[curr_x][curr_y] == word.charAt(0) && !visited[curr_x][curr_y]) {
                    String s = word.substring(1);
                    sol |= dfs(curr_x, curr_y, s, visited, board);
                }
            }
        }
 
        visited[x][y] = false;
        return sol;
    }
 
    public List<String> findWords(char[][] board, String[] words) {
        List<String> ans = new ArrayList<>();
        boolean[][] visited = new boolean[board.length][board[0].length];
 
        for (String word : words) {
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[0].length; j++) {
                    if (board[i][j] == word.charAt(0)) {
                        String s = word.substring(1);
                        if (dfs(i, j, s, visited, board)) {
                            ans.add(word + " -> {" + i + "," + j + "}");
                        }
                    }
                }
            }
        }
 
        return ans;
    }
}
 
class Main {
    public static void main(String[] args) {
        char[][] board = {{'o', 'a', 'a', 'n'},
                          {'e', 't', 'a', 'e'},
                          {'i', 'h', 'k', 'r'},
                          {'i', 'f', 'l', 'v'}};
        String[] words = {"oath", "pea", "eat", "rain"};
        Solution solver = new Solution();
        List<String> result = solver.findWords(board, words);
 
        for (String str : result) {
            System.out.println(str);
        }
    }
}
 
// This code is contributed by Prince Kumar


Python3




class Solution:
    def __init__(self):
        # Possible moves
        self.mover = [(1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
 
    def dfs(self, x, y, word, visited):
        # If word length becomes 0, the string is found
        if len(word) == 0:
            return True
         
        visited[x][y] = True
        sol = False
         
        # Making possible moves
        for move in self.mover:
            curr_x = move[0] + x
            curr_y = move[1] + y
             
            # Checking for out of bound areas
            if 0 <= curr_x < len(self.board) and 0 <= curr_y < len(self.board[0]):
                # Checking for similarity in the first letter and the visited array
                if self.board[curr_x][curr_y] == word[0] and not visited[curr_x][curr_y]:
                    s = word[1:]
                    sol |= self.dfs(curr_x, curr_y, s, visited)
                     
        visited[x][y] = False
        return sol
 
    def findWords(self, board, words):
        self.board = board
        ans = []
        visited = [[False for _ in range(len(board[0]))] for _ in range(len(board))]
         
        for word in words:
            for i in range(len(board)):
                for j in range(len(board[0])):
                    if board[i][j] == word[0]:
                        s = word[1:]
                        if self.dfs(i, j, s, visited):
                            ans.append(f"{word} -> {{{i},{j}}}")
                             
        return ans
 
# Test
board = [['o', 'a', 'a', 'n'],
         ['e', 't', 'a', 'e'],
         ['i', 'h', 'k', 'r'],
         ['i', 'f', 'l', 'v']]
words = ["oath", "pea", "eat", "rain"]
solver = Solution()
print(solver.findWords(board, words))


C#




using System;
using System.Collections.Generic;
 
class Solution {
    // Possible moves
    private int[][] mover
        = { new int[] { 1, 0 },  new int[] { 0, 1 },
            new int[] { -1, 0 }, new int[] { 0, -1 },
            new int[] { 1, 1 },  new int[] { -1, -1 },
            new int[] { 1, -1 }, new int[] { -1, 1 } };
 
    private bool dfs(int x, int y, string word,
                     bool[][] visited, char[][] board)
    {
        // If word length becomes 0, the string is found
        if (word.Length == 0) {
            return true;
        }
 
        visited[x][y] = true;
        bool sol = false;
 
        // Making possible moves
        foreach(int[] move in mover)
        {
            int curr_x = move[0] + x;
            int curr_y = move[1] + y;
 
            // Checking for out of bound areas
            if (0 <= curr_x && curr_x < board.Length
                && 0 <= curr_y
                && curr_y < board[0].Length) {
                // Checking for similarity in the first
                // letter and the visited array
                if (board[curr_x][curr_y] == word[0]
                    && !visited[curr_x][curr_y]) {
                    string s = word.Substring(1);
                    sol |= dfs(curr_x, curr_y, s, visited,
                               board);
                }
            }
        }
 
        visited[x][y] = false;
        return sol;
    }
 
    public IList<string> FindWords(char[][] board,
                                   string[] words)
    {
        IList<string> ans = new List<string>();
        bool[][] visited = new bool[board.Length][];
 
        for (int i = 0; i < board.Length; i++) {
            visited[i] = new bool[board[0].Length];
        }
 
        for (int i = 0; i < words.Length; i++) {
            for (int j = 0; j < board.Length; j++) {
                for (int k = 0; k < board[0].Length; k++) {
                    if (board[j][k] == words[i][0]) {
                        string s = words[i].Substring(1);
                        if (dfs(j, k, s, visited, board)) {
                            ans.Add(
                                $
                                "{words[i]} -> {{{j},{k}}}");
                        }
                    }
                }
            }
        }
 
        return ans;
    }
}
 
class Program {
    static void Main(string[] args)
    {
        char[][] board
            = { new char[] { 'o', 'a', 'a', 'n' },
                new char[] { 'e', 't', 'a', 'e' },
                new char[] { 'i', 'h', 'k', 'r' },
                new char[] { 'i', 'f', 'l', 'v' } };
        string[] words = { "oath", "pea", "eat", "rain" };
 
        Solution solver = new Solution();
        IList<string> ans = solver.FindWords(board, words);
 
        foreach(string part in ans)
        {
            Console.WriteLine(part);
        }
    }
}
// This code is contributed by user_dtewbxkn77n


Javascript




class Solution {
constructor() {
// Possible moves
this.mover = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [-1, -1], [1, -1], [-1, 1]];
}
dfs(x, y, word, visited) {
    // If word length becomes 0, the string is found
    if (word.length == 0) {
        return true;
    }
 
    visited[x][y] = true;
    let sol = false;
 
    // Making possible moves
    for (let move of this.mover) {
        let curr_x = move[0] + x;
        let curr_y = move[1] + y;
 
        // Checking for out of bound areas
        if (0 <= curr_x && curr_x < this.board.length && 0 <= curr_y && curr_y < this.board[0].length) {
            // Checking for similarity in the first letter and the visited array
            if (this.board[curr_x][curr_y] == word[0] && !visited[curr_x][curr_y]) {
                let s = word.slice(1);
                sol |= this.dfs(curr_x, curr_y, s, visited);
            }
        }
    }
 
    visited[x][y] = false;
    return sol;
}
 
findWords(board, words) {
    this.board = board;
    let ans = [];
    let visited = Array.from(Array(board.length), () => new Array(board[0].length).fill(false));
 
    for (let word of words) {
        for (let i = 0; i < board.length; i++) {
            for (let j = 0; j < board[0].length; j++) {
                if (board[i][j] == word[0]) {
                    let s = word.slice(1);
                    if (this.dfs(i, j, s, visited)) {
                        ans.push(`${word} -> {${i},${j}}`);
                    }
                }
            }
        }
    }
 
    return ans;
}
}
// Test
let board = [['o', 'a', 'a', 'n'],
['e', 't', 'a', 'e'],
['i', 'h', 'k', 'r'],
['i', 'f', 'l', 'v']];
let words = ["oath", "pea", "eat", "rain"];
let solver = new Solution();
console.log(solver.findWords(board, words));


Output

['oath -> {0,0}', 'eat -> {1,0}', 'eat -> {1,3}']

The time complexity of the findWords function is O(N * M * L * 8^L), where N is the number of rows in the board, M is the number of columns in the board, L is the maximum length of the word in the dictionary. For each starting point on the board, the algorithm may explore up to 8 directions, and at each step, it needs to check if the next letter in the word matches the character in the board. Since the maximum length of the word is L, this gives a factor of L in the time complexity.

The space complexity of the algorithm is O(N * M + L), where N and M are the dimensions of the board, and L is the length of the longest word in the dictionary. The space is used to store the visited array, which has the same size as the board, and the recursion stack, which can be as deep as the length of the longest word in the dictionary.

Exercise: The above solution only print locations of word. Extend it to print the direction where word is present.
See this for solution of exercise.
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments