Given an m x n board&nbsp;of characters and a list of strings words, return all words on the board.

Each word must 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 in a word.

&nbsp;
Example 1:

Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]


Example 2:

Input: board = [["a","b"],["c","d"]], words = ["abcb"]
Output: []


&nbsp;
Constraints:


	m == board.length
	n == board[i].length
	1 &lt;= m, n &lt;= 12
	board[i][j] is a lowercase English letter.
	1 &lt;= words.length &lt;= 3 * 104
	1 &lt;= words[i].length &lt;= 10
	words[i] consists of lowercase English letters.
	All the strings of words are unique.


class Solution:
    def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
        solution = set()
        trie = self.make_trie(words)
        visited = set()
        for i in range(len(board)):
            for j in range(len(board[0])):
                self.dfs(i,j,board,trie,visited,"",solution)
        return solution
    
    def dfs(self,i,j,board,trie,visited,word,solution):
        if "*" in trie:
            if len(trie.keys()) == 0:
                return
            else:
                solution.add(word)
                del trie["*"]
        if (i,j) in visited:
            return
        if (i < 0 or i == len(board) or j < 0 or j == len(board[0])):
            return
        if board[i][j] not in trie:
            return
        if len(trie[board[i][j]]) == 0:
            del trie[board[i][j]]
            return
        visited.add((i,j))
        neighbours = [(i,j-1),(i-1,j),(i,j+1),(i+1,j)]
        for n_x,n_y in neighbours:
            self.dfs(n_x,n_y,board,trie[board[i][j]],visited,word+board[i][j],solution)    
        visited.remove((i,j))
        
    def make_trie(self,words):
        trie = {}
        for word in words:
            current = trie
            for char in word:
                if char not in current:
                    current[char] = {}
                current = current[char]
            current["*"] = "*"
        return trie
class Solution {
    class TrieNode {
        Map<Character, TrieNode> children = new HashMap();
        boolean word = false;
        public TrieNode() {}
    }
    
    int[][] dirs = new int[][]{{0,1},{0,-1},{-1,0},{1,0}};
    
    public List<String> findWords(char[][] board, String[] words) {
        Set<String> res = new HashSet<>();
        TrieNode root = new TrieNode();
        int m = board.length;
        int n = board[0].length;
        
        for(String word : words){
            char[] cArr = word.toCharArray();
            TrieNode dummy = root;
            
            for(char c : cArr){
                if(!dummy.children.containsKey(c)){
                    dummy.children.put(c, new TrieNode());
                }
                dummy = dummy.children.get(c);
            }
            
            dummy.word = true;
        }
        
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                char nextChar = board[i][j];
                boolean[][] visited = new boolean[m][n];
                if(root.children.containsKey(nextChar)){
                    res.addAll(dfs(board, root.children.get(nextChar), i, j, visited, ""+nextChar));
                }
            }
        }
        
        return new ArrayList<>(res);
    }
    
    Set<String> dfs(char[][] board, TrieNode root, int i, int j, boolean[][] visited, String word){
        Set<String> res = new HashSet<>();
        
        if(root.word){
            res.add(word);
            root.word = false;
        }
        
        visited[i][j] = true;
        
        for(int[] dir : dirs){
            int newI = i + dir[0];
            int newJ = j + dir[1];
            
            if(newI>=0 && newI<board.length && newJ>=0 && newJ<board[0].length && !visited[newI][newJ]){
                char nextChar = board[newI][newJ];
                if(root.children.containsKey(nextChar)){
                   
                    res.addAll(dfs(board, root.children.get(nextChar), newI, newJ, visited, word+nextChar));
                    
                }
            }
        }
        
        visited[i][j] = false;
        
        return res;
    }
}
class Solution {
public:
    vector<string> result;

    struct Trie {
        Trie *child[26];
        bool isEndOfWord;
        string str;

        Trie(){
            isEndOfWord = false;
            str = "";
            for(int i=0; i<26; i++)
                child[i] = NULL;
        }
    };

    Trie *root = new Trie();

    void insert(string &word) {

        Trie *curr = root;

        for(int i=0; i<word.size(); i++) {
            int index = word[i]-'a';

            if(!curr->child[index])
                curr->child[index] = new Trie();

            curr = curr->child[index];
        }

        curr->isEndOfWord = true;
        curr->str = word;
    }

    void trieSearchDFS(vector<vector<char>>& board, Trie *curr, int i, int j, int row, int col) {

        if(i<0 || i>row || j<0 || j>col || board[i][j] == '@')
            return;

        //int index = board[i][j]-'a';
        curr = curr->child[board[i][j]-'a'];

        if(curr == NULL)
            return;

        if(curr->isEndOfWord){
            result.push_back(curr->str);
            curr->isEndOfWord = false;
        }

        char ch = board[i][j];
        board[i][j] = '@';

        if(i-1>=0)
            trieSearchDFS(board,curr,i-1,j,row,col);
        if(j+1<col)
            trieSearchDFS(board,curr,i,j+1,row,col);
        if(i+1<row)
            trieSearchDFS(board,curr,i+1,j,row,col);
        if(j-1>=0)
            trieSearchDFS(board,curr,i,j-1,row,col);

        board[i][j] = ch;

    }

    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        int row = board.size();
        int col = board[0].size();

        for(int i=0; i<words.size(); i++)
            insert(words[i]);

        for(int i=0; i<row; i++){
            for(int j=0; j<col; j++){
                trieSearchDFS(board,root,i,j,row,col);
            }
        }
       return result;
    }
};
const buildTrie = (words) => {
    const trie = {};
    const addToTrie = (word, index = 0, node = trie) => {
        const char = word[index];
        if(!node[char]) {
            node[char] = {};
        }

        if(index === word.length - 1) {
            node[char].word = word;
            return word;
        }

        return addToTrie(word, index + 1, node[char]);
    };

    words.map((word) => addToTrie(word));
    return trie;
};

const dfs = (i, j, board, node, wordsFound = []) => {
    if(i < 0 || i >= board.length) return wordsFound;
    if(j < 0 || j >= board[board.length - 1].length) return wordsFound;

    const char = board[i][j];
    if(char === '#') return wordsFound;

    if(node[char]) {
        if(node[char].word) {
            wordsFound.push(node[char].word);
            node[char].word = null;
        }

        board[i][j] = '#';
        dfs(i + 1, j, board, node[char], wordsFound);
        dfs(i, j + 1, board, node[char], wordsFound);
        dfs(i - 1, j, board, node[char], wordsFound);
        dfs(i, j - 1, board, node[char], wordsFound);
        board[i][j] = char;
    }

    return wordsFound;
};

var findWords = function(board, words) {
    const m = board.length;
    const n = board[m - 1].length;
    const trie = buildTrie(words);

    let result = [];
    for(let i = 0; i < m; i += 1) {
        for(let j = 0; j < n; j += 1) {
            result = result.concat(dfs(i, j, board, trie));
        }
    }
    return result;
};
Word Search II