Skip to content

Conversation

Sabra-jain
Copy link

@Sabra-jain Sabra-jain commented Apr 3, 2024

JAVA CODE IS HERE
class Solution {
int l,m,n;
int[][] directions = {{0,1},{0,-1},{1,0},{-1,0}};
public boolean find(char[][] board,int i,int j,String word,int idx){
if(idx >= l)
return true;

    if(i < 0 || i >= m || j < 0 || j >= n || board[i][j] != word.charAt(idx))
        return false;
    
    char temp = board[i][j];
    board[i][j] = '$';
    
    for(int[] dir : directions) {
        int i_ = i + dir[0];
        int j_ = j + dir[1];
        
        if(find(board, i_, j_, word, idx+1))
            return true;
    }
    
    board[i][j] = temp;
    return false;
}
public boolean exist(char[][] board, String word) {
    m = board.length;
    n = board[0].length;
    l = word.length();
    if(m*n < 1)
        return false;
    for(int i = 0; i<m; i++) {
        for(int j = 0; j<n; j++) {
            if(board[i][j] == word.charAt(0) && find(board, i, j, word, 0)) {
                return true;
            }
        }
    }
    
    return false;
}

}

@Sabra-jain Sabra-jain closed this Apr 3, 2024
@Sabra-jain Sabra-jain reopened this Apr 3, 2024
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.

1 participant