211. Add and Search Word - Data structure design

原题链接:https://leetcode.com/problems/add-and-search-word-data-structure-design/description/
昨天在做 14. Longest Common Prefix 这道题目时,官方解答最后提及了前缀树这种数据结构,然后我就去看了下前缀树的实现的文章 https://leetcode.com/articles/implement-trie-prefix-tree/。这道题目则是前缀树相关题目之一。
虽然我看懂了前缀树,但是并没有独立思考出这道题目,因为这道题目里面涉及到一个通配符的问题。最终看了下讨论区的答案,是使用递归解决这个问题的。哎,虽然中间我也想到了使用递归的思路,但是并没有写出实现来,究其原因大概还是因为思路不够清晰吧!下面就把这位大神的实现抄袭一遍吧:

/**
 * Created by clearbug on 2018/3/6.
 */
public class WordDictionary {

    class TrieNode {
        // R links to node children
        private TrieNode[] links;

        private final int R = 26;

        private boolean isEnd;

        public TrieNode() {
            links = new TrieNode[R];
        }

        public boolean containsKey(char ch) {
            return links[ch - 'a'] != null;
        }

        public TrieNode get(char ch) {
            return links[ch - 'a'];
        }

        public void put(char ch, TrieNode node) {
            links[ch - 'a'] = node;
        }

        public void setEnd() {
            isEnd = true;
        }

        public boolean isEnd() {
            return isEnd;
        }

        public TrieNode[] getLinks() {
            return links;
        }
    }

    private TrieNode root;

    /** Initialize your data structure here. */
    public WordDictionary() {
        root = new TrieNode();
    }

    /** Adds a word into the data structure. */
    public void addWord(String word) {
        TrieNode node = root;
        for (int i = 0; i < word.length(); i++) {
            char currentChar = word.charAt(i);
            if (!node.containsKey(currentChar)) {
                node.put(currentChar, new TrieNode());
            }
            node = node.get(currentChar);
        }
        node.setEnd();
    }

    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    public boolean search(String word) {
        return match(word.toCharArray(), 0, root);
    }
    
    private boolean match(char[] chars, int k, TrieNode node) {
        if (k == chars.length) {
            return node.isEnd();
        }
        char currentChar = chars[k];
        if (currentChar == '.') {
            for (int j = 0; j < node.getLinks().length; j++) {
                if (node.getLinks()[j] != null) {
                    if (match(chars, k + 1, node.getLinks()[j])) {
                        return true;
                    }
                }
            }
        } else {
            if (node.containsKey(currentChar)) {
                return match(chars, k + 1, node.get(currentChar));
            } else {
                return false;
            }
        }
        return false;
    }

    public static void main(String[] args) {

        /**
         * addWord("bad")
         addWord("dad")
         addWord("mad")
         search("pad") -> false
         search("bad") -> true
         search(".ad") -> true
         search("b..") -> true
         */

        WordDictionary obj = new WordDictionary();

        obj.addWord("bad");
        obj.addWord("dad");
        obj.addWord("mad");
        System.out.println(obj.search("pad"));
        System.out.println(obj.search("bad"));
        System.out.println(obj.search(".ad"));
        System.out.println(obj.search("b.."));
        System.out.println(obj.search("abc"));
        System.out.println(obj.search("mad"));
        System.out.println(obj.search("madm"));
    }
}
posted @ 2018-03-06 11:34  optor  阅读(112)  评论(0编辑  收藏  举报