211. Add and Search Word - Data structure design

栏目: 数据库 · 发布时间: 7年前

内容简介:Design a data structure that supports the following two operations:void addWord(word)bool search(word)

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.

Example:

addWord("bad")

addWord("dad")

addWord("mad")

search("pad") -> false

search("bad") -> true

search(".ad") -> true

search("b..") -> true

Note:

You may assume that all words are consist of lowercase letters a-z.

难度:medium

题目:

设计数据结构支持以下两操作:

void addWord(word)

bool search(word)

search(word) 可以查单词或包含.的正则表达式。 .可以表示任何单个字符。

注意:

你可以假定所有单词仅由小写字母组成。

思路:

Tire Tree, DFS

Runtime: 94 ms, faster than 89.57% of Java online submissions for Add and Search Word - Data structure design.

class WordDictionary {
    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) {
        char c = word.charAt(0);
        int i = 0, idx = 0;
        TrieNode ptr = root;
        for (; i < word.length(); i++, ptr = ptr.next[idx]) {
            idx = word.charAt(i) - 'a';
            if (null == ptr.next[idx]) {
                ptr.next[idx] = new TrieNode();
            }
        }
        
        ptr.isWord = true;
    }
    
    // dfs 
    /** 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);
    }
    
    public boolean search(String word, int idx, TrieNode beginNode) {
        if (idx >= word.length() || null == beginNode) {
            return false;
        }
        
        TrieNode node = null;
        int c = word.charAt(idx), cIdx = c - 'a';
        if ('.' == c) {
            boolean result = false;
            for (int i = 0; i < 26; i++) {
                node = beginNode.next[i];
                if (null == node) {
                    continue;
                }
                if ((idx + 1) == word.length() && node.isWord) {
                    return true;   
                }
                result = result || search(word, idx + 1, beginNode.next[i]);
            }
            return result;
        }
        
        // c is not equal to '.'
        node = beginNode.next[cIdx];
        if (null == node) {
            return false;
        }
        if ((idx + 1) == word.length()) {
            return node.isWord;
        }
        
        return search(word, idx + 1, beginNode.next[cIdx]);
    }
    
    public static class TrieNode {
        public boolean isWord;
        public TrieNode[] next = new TrieNode[26];
    }
}

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

JSP网站开发典型模块与实例精讲

JSP网站开发典型模块与实例精讲

李振捷 / 电子工业出版社 / 2006-8 / 50.0

本书是典型模块与实例精讲丛书中的一本。 本书讲解了使用JSP开发网站系统的经典模块和工程实例,基本囊括了JSP的重点技术,对这些模块稍加修改就可以直接使用到实际项目中。为了方便本书的读者交流在学习中遇到的问题,特地在本书的服务网站上公布了很多QQ群组,读者只要拥有QQ号码,就可以参与到本书的QQ学习群组中一起讨论学习心得。本书的作者还在一定的时间给读者提供在线答疑服务。一起来看看 《JSP网站开发典型模块与实例精讲》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具