diff --git a/Backtracking/Word Search.cpp b/Backtracking/Word Search.cpp index 46bdb963..42e6de2c 100644 --- a/Backtracking/Word Search.cpp +++ b/Backtracking/Word Search.cpp @@ -48,3 +48,46 @@ class Solution { return false; } }; + +----JAVA CODE---- + 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