Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

For example, Given words = ["oath","pea","eat","rain"] and board =

[ ['o','a','a','n'], ['e','t','a','e'], ['i','h','k','r'], ['i','f','l','v'] ]

Return ["eat","oath"].

Note: You may assume that all inputs are consist of lowercase letters a-z.

click to show hint.

You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier?

If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How about a Trie? If you would like to learn how to implement a basic trie, please work on this problem: Implement Trie (Prefix Tree) first.

Solution

public class Solution {

    public List<String> findWords(char[][] board, String[] words) {
        List<String> result = new ArrayList<>();
        if (board == null || board.length == 0) return result;

        // build the Trie
        Trie trie = new Trie();
        for (String s : words) {
            trie.addString(s);
        }


        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                help(board, i, j, trie.root, new StringBuffer(), result);
            }
        }

        return result;
    }

    private void help(char[][] board, int x, int y, Node root, StringBuffer sb, List<String> result) {
        if (root == null) return;
        if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) return;

        char cur = board[x][y];
        if (cur == '#') return;
        if (!root.map.containsKey(cur)) return;

        char temp = cur;
        board[x][y] = '#';

        sb.append(cur);

        Node nextNode = root.map.get(cur);
        if (nextNode.flag) {
            result.add(sb.toString());
                        nextNode.flag = false;

        }
        help(board, x + 1, y, nextNode, sb, result);
        help(board, x - 1, y, nextNode, sb, result);
        help(board, x, y + 1, nextNode, sb, result);
        help(board, x, y - 1, nextNode, sb, result);
        sb.deleteCharAt(sb.length() - 1);
        board[x][y] = temp;
    }


    private class Node {
        boolean flag;
        char cur;
        Map<Character, Node> map;

        public Node(char cur) {
            this.cur = cur;
            map = new HashMap<>();
        }
    }

    private class Trie {
        Node root;

        public Trie() {
            this.root = new Node(' ');
        }

        public void addString(String s) {
            if (s == null || s.length() == 0) return;

            Node currentNode = root;
            for (int i = 0; i < s.length(); i++) {
                char currentChar = s.charAt(i);
                if (!currentNode.map.containsKey(currentChar)) {
                    currentNode.map.put(currentChar, new Node(currentChar));
                }
                currentNode = currentNode.map.get(currentChar);

                if (i == s.length() - 1) {
                    currentNode.flag = true;
                }
            }
        }
    }
}

results matching ""

    No results matching ""