zoukankan      html  css  js  c++  java
  • Add and Search Word

    Add and Search Word - Data structure design

    问题:

    Design a data structure that supports the following two operations:

    void addWord(word)
    bool search(word)
    

    search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

    思路:

      前缀树+回溯

    我的代码:

    public class WordDictionary {
    
        private TrieNode root;
    
        public WordDictionary() {
            root = new TrieNode();
        }
        public void addWord(String word) {
            HashMap<Character, TrieNode> children = root.children;
            for(int i=0; i<word.length(); i++)
            {
                TrieNode node;
                char c = word.charAt(i);
                if(children.containsKey(c)) node = children.get(c);
                else
                {
                    node = new TrieNode(c);
                    children.put(c,node);
                }
                children = node.children;
                if(i == word.length()-1)
                {
                    node.isWord = true;
                }
            }
        }
        public boolean search(String word) {
            if(word==null || word.length()==0)    return true;
            HashMap<Character, TrieNode> children = root.children;
            return helperSearch(word, children);
            
        }
        public boolean helperSearch(String word, HashMap<Character, TrieNode> hash)
        {
            if(word.length() == 1)
            {
                char c = word.charAt(0);
                if(c == '.')
                {
                    for(char key : hash.keySet())
                    {
                        if(hash.get(key).isWord) return true;
                    }
                    return false;
                }
                else
                {
                    if(hash.containsKey(c))        return hash.get(c).isWord;
                    else return false;
                }
            }
            if(word.charAt(0) == '.')
            {
                for(char c: hash.keySet())
                {
                    if(helperSearch(word.substring(1), hash.get(c).children))   return true;   
                }
                return false;
            }
            else
            {
                char c = word.charAt(0);
                if(hash.containsKey(c))
                {
                    return helperSearch(word.substring(1), hash.get(c).children);
                }
                else return false;
            }
        }
        class TrieNode {
            public TrieNode() {
            }
            public TrieNode(char c) {
                this.c = c;
            }
            char c;
            HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>();  
            boolean isWord;
        }
    }
    View Code

    学习之处:

    • 之前做了前缀树的解法,很自然的考虑到了回溯的解法,一遍AC了
  • 相关阅读:
    四人过桥求最短时间(《算法设计与分析》习题一的第8题) Apare_xzc
    已知中序后序遍历还原二叉树(uva 548) Apare_xzc
    大二下学期第四周周报(2019.3.24) Apare_xzc
    大二下学期第五周周报(2019.3.31) Apare_xzc
    拓扑排序复习(处女座的比赛资格)
    exgcd复习 Apare_xzc
    ACM课程设计课 Advance_Contest_1 解题报告 Apare_xzc
    使用共享内存
    使用信号量
    使用命名管道
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4543293.html
Copyright © 2011-2022 走看看