211.Add and Search Word

17 篇文章 0 订阅
10 篇文章 0 订阅

该题有两种思路:

one:利用Trie树实现:

class WordDictionary {
    
    private class Trie {
        public boolean isWord;
        public Trie[] children;
        public Trie() {
            this.isWord = false;
            this.children = new Trie[26];
        }
    }
    
    private Trie root;

    /** Initialize your data structure here. */
    public WordDictionary() {
        root = new Trie();
    }
    
    /** Adds a word into the data structure. */
    public void addWord(String word) {
        Trie node = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (node.children[c - 'a'] == null) node.children[c - 'a'] = new Trie();
            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 search(word, 0, root);
    }
    
    // word may contain . for wild card matching
    private boolean search(String word, int start, Trie node) {
        if (start == word.length()) return node.isWord;
        char c = word.charAt(start);
        if (c == '.') {
            for (int i = 0; i < 26; i++) {
                if (node.children[i] != null) {
                    if (search(word, start + 1, node.children[i])) return true;
                }
            }
            return false;
        } else {
            if (node.children[c - 'a'] == null) return false;
            return search(word, start + 1, node.children[c - 'a']);
        }
    }
    
}
class WordDictionary {
    
 public class TrieNode {
        public TrieNode[] children = new TrieNode[26];
        public String item = "";
    }
    
    private TrieNode root = new TrieNode();

    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.item = word;
    }

    public boolean search(String word) {
        return match(word.toCharArray(), 0, root);
    }
    
    private boolean match(char[] chs, int k, TrieNode node) {
        if (k == chs.length) return !node.item.equals("");   
        if (chs[k] != '.') {
            return node.children[chs[k] - 'a'] != null && match(chs, k + 1, node.children[chs[k] - 'a']);
        } else {
            for (int i = 0; i < node.children.length; i++) {
                if (node.children[i] != null) {
                    if (match(chs, k + 1, node.children[i])) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */

 

 two:HashMap法:

//fastest solution
class WordDictionary {
    private HashMap<Integer, List<String>> map;
    /** Initialize your data structure here. */
    public WordDictionary() {
        map = new HashMap<>();
    }
    
    /** Adds a word into the data structure. */
    public void addWord(String word) {
        int length = word.length();
        if(map.containsKey(length)){
            map.get(length).add(word);
        }else{
            map.put(length, new ArrayList<>());
            map.get(length).add(word);
        }
    }
    
    /** 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) {
        int length = word.length();
        if(!map.containsKey(length)){
            return false;
        }else{
            List<String> list = map.get(length);
            for(String cur : list){
                if(isSame(cur,word)) return true;
            }
            return false;
        }
    }
    public boolean isSame(String s1, String s2){
        int n = s1.length();
        for(int i = 0; i < n; i++){
            if(s2.charAt(i) == '.') continue;
            else{
                if(s1.charAt(i) != s2.charAt(i)) return false;
            }
        }
        return true;
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值