Question

Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

Solution

A trie node should contains the character, its children and the flag that marks if it is a leaf node.

Trie is an efficient information retrieval data structure. Using trie, search complexities can be brought to optimal limit (key length).

Every node of trie consists of multiple branches. Each branch represents a possible character of keys. We need to mark the last node of every key as leaf node.

We design TrieNode to store 1. value 2. isLeaf 3. a map of children (map is used for quick selection)

In this way, we can implement Trie start from root. Add new child to original parent is implemented by put action in map.

Note here, search and startsWith differs.

  null

   |

   a

   |

   b

for "a", search will return false, while startsWith will return true.

 /**
* Your Trie object will be instantiated and called as such:
* Trie trie = new Trie();
* trie.insert("lintcode");
* trie.search("lint"); will return false
* trie.startsWith("lint"); will return true
*/
class TrieNode {
char val;
boolean isLast;
Map<Character, TrieNode> children;
// Initialize your data structure here.
public TrieNode(char val) {
children = new HashMap<Character, TrieNode>();
this.val = val;
this.isLast = false;
}
} public class Trie {
private TrieNode root; public Trie() {
root = new TrieNode(' ');
} // Inserts a word into the trie.
public void insert(String word) {
if (word == null || word.length() == 0) {
return;
}
TrieNode p = this.root;
char[] arr = word.toCharArray();
for (char cur : arr) {
if (!p.children.containsKey(cur)) {
p.children.put(cur, new TrieNode(cur));
}
p = p.children.get(cur);
}
p.isLast = true;
} // Returns if the word is in the trie.
public boolean search(String word) {
if (word == null || word.length() == 0) {
return false;
}
char[] arr = word.toCharArray();
TrieNode p = this.root;
for (char cur : arr) {
Map<Character, TrieNode> children = p.children;
if (!children.containsKey(cur)) {
return false;
}
p = children.get(cur);
}
return p.isLast;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
if (prefix == null || prefix.length() == 0) {
return false;
}
char[] arr = prefix.toCharArray();
TrieNode p = this.root;
for (char cur : arr) {
Map<Character, TrieNode> children = p.children;
if (!children.containsKey(cur)) {
return false;
}
p = children.get(cur);
}
return true;
}
}

Implement Trie (Prefix Tree) 解答的更多相关文章

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

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

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

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

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

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

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

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

  5. 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  ...

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

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

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

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

  8. Leetcode: Implement Trie (Prefix Tree) && Summary: Trie

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

  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. CF 584B Kolya and Tanya

    题目大意:3n个人围着一张桌子,给每个人发钱,可以使1块.2块.3块,第i个人的金额为Ai.若存在第个人使得Ai + Ai+n + Ai+2n != 6,则该分配方案满足条件,求所有的满足条件的方案数 ...

  2. hihoCoder 1041 国庆出游 (DFS)

    题意: 小Hi和小Ho准备国庆期间去A国旅游.A国的城际交通比较有特色:它共有n座城市(编号1-n):城市之间恰好有n-1条公路相连,形成一个树形公路网.小Hi计划从A国首都(1号城市)出发,自驾遍历 ...

  3. 如何在程序中调用Caffe做图像分类

    Caffe是目前深度学习比较优秀好用的一个开源库,采样c++和CUDA实现,具有速度快,模型定义方便等优点.学习了几天过后,发现也有一个不方便的地方,就是在我的程序中调用Caffe做图像分类没有直接的 ...

  4. UVa1587.Digit Counting

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=247&p ...

  5. oracle 12g sqlplus安装

    一.下载oracle 12g sqlplus软件 linux 64位操作系统,oracle安装包地址 http://www.oracle.com/technetwork/topics/linuxx86 ...

  6. iOS-网络编程(一)HTTP协议

    一. 网络编程基础 在移动互联网时代,几乎所有应用都需要用到网络,只有通过网络跟外界进行数据交互.数据更新,应用才能保持新鲜.活力.一个好的移动网络应用不仅要有良好的UI和良好的用户体验也要具备实时更 ...

  7. Apache POI组件操作Excel,制作报表(二)

    本文接上一篇继续探究POI组件的使用.     现在来看看Excel的基本设置问题,以2007为例,先从工作簿来说,设置列宽,因为生成表格列应该固定,而行是遍历生成的,所以可以在工作簿级别来设置列宽, ...

  8. 小贝_mysql建表以及列属性

    mysql建表以及列属性 简要: 一.建表原则 二.具体的列属性说明 一.建表原则 建表: 事实上就是声明列的过程,数据终于是以文件的形式放在硬盘(内存) 列: 不同的列类型占的空间不一样. 选列的原 ...

  9. C. Robot(BFS)

    C. Robot Time Limit: 3000ms Case Time Limit: 3000ms Memory Limit: 262144KB 64-bit integer IO format: ...

  10. Linux用户和用户组

    用户分类 按位置分:本地账户.远程账户 按功能分:普通用户.超级用户(root) 普通用户: (1)系统用户:UID 1-499 (2)本地用户:UID 500+ 每一个用户,都有一个同名的用户组. ...