Problem Link

Description


Given an m x n boardΒ 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.

Β 

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: []

Β 

Constraints:

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

Solution


C++

class Solution {
    struct TrieNode {
        TrieNode *children[26];
        string word;
 
        TrieNode() : word("") {
            for (int i = 0; i < 26; i++) {
                children[i] = nullptr;
            }
        }
    };
 
public:
    vector<string> findWords(vector<vector<char>> &board, vector<string> &words) {
        TrieNode *root = buildTrie(words);
        vector<string> result;
        for (int i = 0; i < board.size(); i++) {
            for (int j = 0; j < board[0].size(); j++) {
                dfs(board, i, j, root, result);
            }
        }
        return result;
    }
 
    /** Inserts a word into the trie. */
    TrieNode *buildTrie(vector<string> &words) {
        TrieNode *root = new TrieNode();
        for (int j = 0; j < words.size(); j++) {
            string word = words[j];
            TrieNode *curr = root;
            for (int i = 0; i < word.length(); i++) {
                char c = word[i] - 'a';
                if (curr->children[c] == nullptr) {
                    curr->children[c] = new TrieNode();
                }
                curr = curr->children[c];
            }
            curr->word = word;
        }
        return root;
    }
 
    void dfs(vector<vector<char>> &board, int i, int j, TrieNode *p, vector<string> &result) {
        char c = board[i][j];
        if (c == '#' || !p->children[c - 'a']) return;
        p = p->children[c - 'a'];
        if (p->word.size() > 0) {
            result.push_back(p->word);
            p->word = "";
        }
 
        board[i][j] = '#';
        if (i > 0) dfs(board, i - 1, j, p, result);
        if (j > 0) dfs(board, i, j - 1, p, result);
        if (i < board.size() - 1) dfs(board, i + 1, j, p, result);
        if (j < board[0].size() - 1) dfs(board, i, j + 1, p, result);
        board[i][j] = c;
    }
};

Python3

class TrieNode:
    def __init__(self):
        self.children = {}
        self.hasEnd = False
    
    def hasEdge(self, x):
        return x in self.children
    
    def get(self, x):
        if not self.hasEdge(x): return None
 
        return self.children[x]
 
class Trie:
    def __init__(self):
        self.root = TrieNode()
    
    def insert(self, word):
        curr = self.root
 
        for x in word:
            if curr.hasEdge(x):
                curr = curr.children[x]
            else:
                curr.children[x] = TrieNode()
                curr = curr.children[x]
            
        curr.hasEnd = True
 
class Solution:
    def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
        rows, cols = len(board), len(board[0])
        trie = Trie()
        visited = set()
        res = []
 
        for word in words:
            trie.insert(word)
 
        def search(x, y, curr, node):
            if node.hasEnd:
                res.append(curr)
                node.hasEnd = False
 
            for dx, dy in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
                if 0 <= dx < rows and 0 <= dy < cols and (dx, dy) not in visited and node.hasEdge(board[dx][dy]):
                    visited.add((dx, dy))
                    search(dx, dy, curr + board[dx][dy], node.get(board[dx][dy]))
                    visited.remove((dx, dy))
 
 
        for x in range(rows):
            for y in range(cols):
                if trie.root.hasEdge(board[x][y][0]):
                    visited.add((x, y))
                    search(x, y, board[x][y][0], trie.root.get(board[x][y]))
                    visited.remove((x, y))
        
        return res