0

LeetCode Problem description here.

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.

Input:

[["a","a","b","a","a","b"],
 ["a","a","b","b","b","a"], 
 ["a","a","a","a","b","a"],
 ["b","a","b","b","a","b"], 
 ["a","b","b","a","b","a"],
 ["b","a","a","a","a","b"]]

word to find :"bbbaabbbbbab"

Output: true

Expected: false

class Solution {
    public boolean exist(char[][] board, String word) {
        if (word.equals(null)) {
            return false;
        }
        Stack<String> path = new Stack<String>();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                boolean foundWord = dfs(board,i, j, 0, word,path);
                if (foundWord == true) {
                    return true;
                }
            }
        }
        
        return false;
    }
    private boolean dfs(char [][] board,int i, int j, int wordIndex, String word, Stack<String> path) {
        if (!path.contains("(" + String.valueOf(i) + "," + String.valueOf(j) + ")"))
        {
            path.push("(" + String.valueOf(i) + "," + String.valueOf(j) + ")");
        }
        if (wordIndex == word.length()) {
          
            return true;
        }
        if (i < 0 || j < 0 || i >= board.length || j >= board[i].length){
            path.pop();
            return false;
        }
        else if (board[i][j] != word.charAt(wordIndex)) {
            path.pop();
            return false;
        }
        char oldLetter = board[i][j];
        board[i][j]='*';
        boolean foundWord = dfs(board,i, j - 1, wordIndex + 1, word,path) || dfs(board,i, j + 1, wordIndex + 1, word,path)
                || dfs(board,i - 1, j, wordIndex + 1, word,path) || dfs(board,i + 1, j, wordIndex + 1, word,path)
                || dfs(board,i + 1, j + 1, wordIndex + 1, word,path);

        board[i][j]=oldLetter;
        return foundWord;   
    }
}

2 Answers2

0

You must search for paths with horizontal and vertical connections only, no diagonal ones. But this ...

                || dfs(board,i + 1, j + 1, wordIndex + 1, word,path);

... allows for paths containing (certain) diagonal links to be accepted.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

In line 37 of your code, you have an extra DFS call dfs(board, i + 1, j + 1, wordIndex + 1, word, path) where you are going diagonally up-right direction. If you remove this then the code logic will be fine.

And I suppose you might be using the paths variable just to debug, the code's complexity is not good right now path.contains() method takes linear time to check if the element is present or not. Due to this statement, the complexity of this approach is high and it will TLE if you try to submit the code without removing it.

class Solution {
    public boolean exist(char[][] board, String word) {
        if (word.equals(null)) {
            return false;
        }
        
        for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[row].length; col++) {
                boolean foundWord = dfs(board, row, col, 0, word);
                if (foundWord == true) {
                    return true;
                }
            }
        }
        
        return false;
    }
    
    private boolean dfs(char [][] board, int row, int col, int wordIndex, String word) {
        if (wordIndex == word.length()) {
            return true;
        }
        
        if (row < 0 || col < 0 || row >= board.length || col >= board[row].length || 
            board[row][col] != word.charAt(wordIndex)
           ) {
            return false;
        }
        
        char oldLetter = board[row][col];
        board[row][col] = '*';
        
        boolean foundWord = dfs(board, row, col - 1, wordIndex + 1, word) || 
            dfs(board, row, col + 1, wordIndex + 1, word) || 
            dfs(board, row - 1, col, wordIndex + 1, word) || 
            dfs(board, row + 1, col, wordIndex + 1, word);

        board[row][col] = oldLetter;
        return foundWord;   
    }
}
sachuverma
  • 559
  • 6
  • 27