zoukankan      html  css  js  c++  java
  • <Trie> 208 211

    208. Implement Trie (Prefix Tree)

    class TrieNode{
        private char val;
        public boolean isWord;
        public TrieNode[] children = new TrieNode[26];
        public TrieNode(){};
        public TrieNode(char c){
            this.val = c;
        }
    }
    class Trie {
        private TrieNode root;
    
        /** Initialize your data structure here. */
        public Trie() {
            root = new TrieNode(' ');
        }
        
        /** Inserts a word into the trie. */
        public void insert(String word) {
            TrieNode cur = root;
            for(int i = 0; i < word.length(); i++){
                char c = word.charAt(i);
                if(cur.children[c - 'a'] == null){
                    cur.children[c - 'a'] = new TrieNode(c);
                }
                cur = cur.children[c - 'a'];
            }
            cur.isWord = true;
        }
        
        /** Returns if the word is in the trie. */
        public boolean search(String word) {
            TrieNode cur = root;
            for(int i = 0; i < word.length(); i++){
                char c = word.charAt(i);
                if(cur.children[c - 'a'] == null) return false;
                cur = cur.children[c - 'a'];
            }
            return cur.isWord;
        }
        
        /** Returns if there is any word in the trie that starts with the given prefix. */
        public boolean startsWith(String prefix) {
            TrieNode cur = root;
            for(int i = 0; i < prefix.length(); i++){
                char c =prefix.charAt(i);
                if(cur.children[c - 'a'] == null) return false;
                cur = cur.children[c - 'a'];
            }
            return true;
        }
    }

    211. Add and Search Word - Data structure design

    class WordDictionary {
        
        public class TrieNode{
            public TrieNode[] children = new TrieNode[26];
            public boolean isWord;
        }
        
        private TrieNode root = new TrieNode();
        /** Initialize your data structure here. */
        public WordDictionary() {
            
        }
        
        /** Adds a word into the data structure. */
        public void addWord(String word) {
            TrieNode node = root;
            for(char c : word.toCharArray()){
                if(node.children[c - 'a'] == null){
                    node.children[c - 'a'] = new TrieNode();
                }
                node = node.children[c - 'a'];
            }
            node.isWord = true;
        }
        
        /** 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[] chs, int start, TrieNode node){
            if(start == chs.length) return node.isWord;
            
            if(chs[start] == '.'){
                for(int i = 0; i < node.children.length; i++){
                    if(node.children[i] != null && match(chs, start + 1, node.children[i]))
                        return true;
                } 
            }else{
                return node.children[chs[start] - 'a'] != null && match(chs, start + 1, node.children[chs[start] - 'a']);
            }
        return false;
        }
    }
  • 相关阅读:
    Mysql一套完整练习题
    Ubuntu kylin优麒麟下配置Hive环境
    win10解决无法远程桌面连接问题(参考)
    OSI七层模型的工作协议划分
    20201116-每日一题
    20201115-福州大学-助教-周总结-第9次
    2020年11月学习记录
    20201104-福州大学-助教-周总结-第7次
    2020年10月学习记录
    20201025-福州大学-助教-周总结-第6次
  • 原文地址:https://www.cnblogs.com/Afei-1123/p/11862829.html
Copyright © 2011-2022 走看看