Skip to content

[N Queens && Word Search]#1085

Open
takchiks wants to merge 2 commits intosuper30admin:masterfrom
takchiks:master
Open

[N Queens && Word Search]#1085
takchiks wants to merge 2 commits intosuper30admin:masterfrom
takchiks:master

Conversation

@takchiks
Copy link

No description provided.

@super30admin
Copy link
Owner

N-Queens (NQueens.java)

  • The solution is correct and efficient.
  • The code is well-written.
  • One minor suggestion: in the isValid method, the expression might be clearer if written as:
    if (col[c] || diaL[r - c + n] || diaR[r + c]) return false; else return true;
    But the current way is also fine.
  • Alternatively, they could write:
    return !col[c] && !diaL[r - c + n] && !diaR[r + c];
    This is equivalent and might be more straightforward.

VERDICT: PASS


Word Search (WordSearch.java)

Strengths:

  • You have correctly identified the need for DFS with backtracking.
  • You used a directions array to explore neighbors, which is a good practice.
  • You marked visited cells and backtracked appropriately.

Areas for improvement:

  1. Initialization of ans: The ans variable should be initialized to false at the start of the exist method. However, it's better to avoid using an instance variable altogether. Instead, make the helper function return a boolean so that you can propagate the result directly.

  2. Handling the base case: Your solution does not handle the case when the word has only one character. You should check inside the exist method if the word length is 0 or 1. For example, if the word has one character, you should return true immediately when you find that character.

  3. Early termination: You should stop the search as soon as the word is found. In your current code, even after setting ans to true, the DFS continues. You should modify the helper function to return boolean and break out of the loop as soon as one path returns true.

  4. Space optimization: Instead of using a separate visited array, you can mark the board cell with a special character (like '#' as in the reference solution) and then backtrack by restoring the original character. This saves space and avoids initializing a new visited array for each call.

  5. Code structure: The helper function should be recursive and return a boolean. This makes the code clearer and more efficient.

Here is a revised version of your code:

class Solution {
    int[][] dirs = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
    
    public boolean exist(char[][] board, String word) {
        int m = board.length;
        int n = board[0].length;
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == word.charAt(0)) {
                    if (dfs(board, i, j, word, 0)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    private boolean dfs(char[][] board, int i, int j, String word, int idx) {
        if (idx == word.length() - 1) {
            return true;
        }
        
        char temp = board[i][j];
        board[i][j] = '#'; // mark as visited
        
        for (int[] dir : dirs) {
            int r = i + dir[0];
            int c = j + dir[1];
            
            if (r >= 0 && r < board.length && c >= 0 && c < board[0].length && board[r][c] == word.charAt(idx+1)) {
                if (dfs(board, r, c, word, idx+1)) {
                    return true;
                }
            }
        }
        
        board[i][j] = temp; // backtrack
        return false;
    }
}

Note: This revised code handles the one-character word by checking if we are at the last character (idx == word.length()-1) before proceeding to neighbors. Also, it uses the board itself for marking visited cells.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants