@@ -0,0 +1,2263 @@
+// Given a 2D board and a word, find if the word exists in the grid.%0A%0A// The word can be constructed from letters of sequentially adjacent cell, where %22adjacent%22 cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.%0A%0A// For example,%0A// Given board =%0A%0A// %5B%0A//   %5B%22ABCE%22%5D,%0A//   %5B%22SFCS%22%5D,%0A//   %5B%22ADEE%22%5D%0A// %5D%0A// word = %22ABCCED%22, -%3E returns true,%0A// word = %22SEE%22, -%3E returns true,%0A// word = %22ABCB%22, -%3E returns false.%0A%0Apublic class Solution %7B%0A    public boolean exist(char%5B%5D%5B%5D board, String word) %7B%0A        int len = word.length();%0A        if(len == 0)%7B%0A        %09return false;%0A        %7D%0A%0A        int row = board.length;%0A        int col = board%5B0%5D.length;%0A        %0A        if(len %3E row * col) return false;%0A%0A        boolean%5B%5D%5B%5D used = new boolean%5Brow%5D%5Bcol%5D;%0A%0A        for(int i = 0; i %3C row; i++)%7B%0A        %09for(int j = 0; j %3C col; j++)%7B%0A        %09%09if(word.charAt(0) == board%5Bi%5D%5Bj%5D)%7B%0A        %09%09%09if(dfs(board, used, i, j, word, 0))%7B%0A        %09%09%09%09return true;%0A        %09%09%09%7D%0A        %09%09%7D%0A        %09%7D%0A        %7D%0A%0A        return false;%0A    %7D%0A%0A    public boolean dfs(char%5B%5D%5B%5D board, boolean%5B%5D%5B%5D used, int row, int col, String word, int index)%7B%0A    %09if(row %3C 0 %7C%7C row %3E board.length - 1 %7C%7C col %3C 0 %7C%7C col %3E board%5B0%5D.length - 1 %7C%7C used%5Brow%5D%5Bcol%5D) return false;%0A    %09if(word.charAt(index) == board%5Brow%5D%5Bcol%5D && !used%5Brow%5D%5Bcol%5D)%7B%0A    %09%09used%5Brow%5D%5Bcol%5D = true;%0A%0A    %09%09if(index == word.length() - 1) return true;%0A%0A    %09%09// LOL only this way JAVA solution will pass. The commented one will TLE.. So you have a idea of JAVA's optimization.%0A    %09%09boolean result = dfs(board, used, row - 1, col, word, index + 1) %7C%7C dfs(board, used, row + 1, col, word, index + 1) %7C%7C dfs(board, used, row, col - 1, word, index + 1) %7C%7C dfs(board, used, row, col + 1, word, index + 1);%0A    %09%09// // go up.%0A    %09%09// boolean up = dfs(board, used, row - 1, col, word, index + 1);%0A    %09%09// // go down.%0A    %09%09// boolean down = dfs(board, used, row + 1, col, word, index + 1);%0A    %09%09// // go left.%0A    %09%09// boolean left = dfs(board, used, row, col - 1, word, index + 1);%0A    %09%09// // go right.%0A    %09%09// boolean right = dfs(board, used, row, col + 1, word, index + 1);%0A%0A    %09%09used%5Brow%5D%5Bcol%5D = false;%0A    %09%09return result;%0A%0A    %09%7Delse%7B%0A    %09%09return false;%0A    %09%7D%0A    %7D%0A%7D

add word search solution