1. Implement a trie with insert, search, and startsWith methods.
  2.  
  3. Note:
  4. You may assume that all inputs are consist of lowercase letters a-z.

参考百度百科:Trie树

trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes)

The time complexity to insert and to search is O(m), where m is the length of the string.

标准Trie树的应用和优缺点

(1) 全字匹配:确定待查字串是否与集合的一个单词完全匹配。如上代码fullMatch()。

(2) 前缀匹配:查找集合中与以s为前缀的所有串。

注意:Trie树的结构并不适合用来查找子串。这一点和前面提到的PAT Tree以及后面专门要提到的Suffix Tree的作用有很大不同。

优点: 查找效率比与集合中的每一个字符串做匹配的效率要高很多。在o(m)时间内搜索一个长度为m的字符串s是否在字典里。Predictable O(k) lookup time where k is the size of the key

缺点:标准Trie的空间利用率不高,可能存在大量结点中只有一个子结点,这样的结点绝对是一种浪费。正是这个原因,才迅速推动了下面所讲的压缩trie的开发。

什么时候用Trie?

It all depends on what problem you're trying to solve. If all you need to do is insertions and lookups, go with a hash table. If you need to solve more complex problems such as prefix-related queries, then a trie might be the better solution.

像word search II就是跟前缀有关,如果dfs发现当前形成的前缀都不在字典中,就没必要再搜索下去了,所以用trie不用hashSet

Easy version of implement Trie. TrieNode only contains TrieNode[] children, and boolean isWord two fields

  1. class Trie {
  2. class TrieNode {
  3. TrieNode[] children;
  4. boolean isWord;
  5. public TrieNode() {
  6. this.children = new TrieNode[26];
  7. this.isWord = false;
  8. }
  9. }
  10.  
  11. TrieNode root;
  12.  
  13. /** Initialize your data structure here. */
  14. public Trie() {
  15. this.root = new TrieNode();
  16. }
  17.  
  18. /** Inserts a word into the trie. */
  19. public void insert(String word) {
  20. if (word == null || word.length() == 0) return;
  21. TrieNode cur = this.root;
  22. for (int i = 0; i < word.length(); i ++) {
  23. if (cur.children[word.charAt(i) - 'a'] == null) {
  24. cur.children[word.charAt(i) - 'a'] = new TrieNode();
  25. }
  26. cur = cur.children[word.charAt(i) - 'a'];
  27. }
  28. cur.isWord = true;
  29. }
  30.  
  31. /** Returns if the word is in the trie. */
  32. public boolean search(String word) {
  33. TrieNode cur = this.root;
  34. for (int i = 0; i < word.length(); i ++) {
  35. if (cur.children[word.charAt(i) - 'a'] == null) return false;
  36. cur = cur.children[word.charAt(i) - 'a'];
  37. }
  38. return cur.isWord;
  39. }
  40.  
  41. /** Returns if there is any word in the trie that starts with the given prefix. */
  42. public boolean startsWith(String prefix) {
  43. TrieNode cur = this.root;
  44. for (int i = 0; i < prefix.length(); i ++) {
  45. if (cur.children[prefix.charAt(i) - 'a'] == null) return false;
  46. cur = cur.children[prefix.charAt(i) - 'a'];
  47. }
  48. return true;
  49. }
  50. }

Older version, TrieNode also has num and val fields, which might not be that useful.

  1. class TrieNode {
  2. // Initialize your data structure here.
  3. int num; //How many words go through this TrieNode
  4. TrieNode[] son; //collection of sons
  5. boolean isEnd;
  6. char val;
  7.  
  8. public TrieNode() {
  9. this.num = 0;
  10. this.son = new TrieNode[26];
  11. this.isEnd = false;
  12. }
  13. }
  14.  
  15. public class Trie {
  16. private TrieNode root;
  17.  
  18. public Trie() {
  19. root = new TrieNode();
  20. }
  21.  
  22. // Inserts a word into the trie.
  23. public void insert(String word) {
  24. if (word==null || word.length()==0) return;
  25. char[] arr = word.toCharArray();
  26. TrieNode node = this.root;
  27. for (int i=0; i<arr.length; i++) {
  28. int pos = (int)(arr[i] - 'a');
  29. if (node.son[pos] == null) {
  30. node.son[pos] = new TrieNode();
  31. node.son[pos].num++;
  32. node.son[pos].val = arr[i];
  33. }
  34. else {
  35. node.son[pos].num++;
  36. }
  37. node = node.son[pos];
  38. }
  39. node.isEnd = true;
  40. }
  41.  
  42. // Returns if the word is in the trie.
  43. public boolean search(String word) {
  44. char[] arr = word.toCharArray();
  45. TrieNode node = this.root;
  46. for (int i=0; i<arr.length; i++) {
  47. int pos = (int)(arr[i] - 'a');
  48. if (node.son[pos] == null) return false;
  49. node = node.son[pos];
  50. }
  51. return node.isEnd;
  52. }
  53.  
  54. // Returns if there is any word in the trie
  55. // that starts with the given prefix.
  56. public boolean startsWith(String prefix) {
  57. char[] arr = prefix.toCharArray();
  58. TrieNode node = this.root;
  59. for (int i=0; i<arr.length; i++) {
  60. int pos = (int)(arr[i] - 'a');
  61. if (node.son[pos] == null) return false;
  62. node = node.son[pos];
  63. }
  64. return true;
  65. }
  66. }
  67.  
  68. // Your Trie object will be instantiated and called as such:
  69. // Trie trie = new Trie();
  70. // trie.insert("somestring");
  71. // trie.search("key");

Leetcode: Implement Trie (Prefix Tree) && Summary: Trie的更多相关文章

  1. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  2. Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...

  3. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  4. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  5. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  6. 【LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  7. 【刷题-LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...

  8. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  9. 【leetcode】208. Implement Trie (Prefix Tree 字典树)

    A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...

随机推荐

  1. Introduction to Structured Data

    https://developers.google.com/search/docs/guides/intro-structured-data Structured data refers to kin ...

  2. (IOS)Swift2.0 Radio 程序分析

    本文主要分享下楼主在学习Swift编程过程中,对GitHub上的一个开源项目Swift Radio的研究心得. 项目地址:https://github.com/swiftcodex/Swift-Rad ...

  3. 非模态对话框的PreTranslateMessage() 没有用,无法进去

    非模态对话框的的PreTranslateMessage确实进不去, 自然也无法用重载PreTranslateMessage的方法来响应键盘消息. 可以用Hook的方法来使其生效. http://bbs ...

  4. 初入C的世界

    大家好,我叫吉贯之,来自贵州省遵义市,现就读于北京工业大学耿丹学院信息技术系计算机与科学专业,我的学号是160809127,我喜欢运动和一些电脑方面的软件操作. 应老师要求在博客园建立的博客,地址是h ...

  5. Select Statement Syntax [AX 2012]

    Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 ...

  6. CentOS7 + linux kernel 3.10.94 compile 简记

    Linux kernel 一直以其开源著称,可以自己编译选择合适的模块,针对特定的系统可以有不同的编译选项 来源 此次编译的内核版本为3.10.94,从官网www.kernel.org下载而来,自己虚 ...

  7. leetcode:Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...

  8. http://www.cnblogs.com/littlemonk/p/5705476.html

    http://www.cnblogs.com/littlemonk/p/5705476.html

  9. NavigationController popToViewController跳转之前任意ViewController方法

    NSArray *viewControllers = self.navigationController.viewControllers;A *viewController = [viewContro ...

  10. placeholder兼容

    <!------------placeholder兼容-------------><script type="text/javascript">    $( ...