LeetCode Implement Trie (Prefix Tree)

原创 2015年07月09日 10:53:11

Description:

Implement a trie with insertsearch, and startsWith methods.

Solution:

Trie树是字符串常用的一个数据结构,AC自动机也会用到。这里需要注意的是,prefix和search功能的不一样,也就要求了要在每个节点中添加一个标记,记录当前节点是否表示一个字符串的结束。

public class Trie {
	private TrieNode root;

	public Trie() {
		root = new TrieNode();
	}

	// Inserts a word into the trie.
	public void insert(String word) {
		TrieNode temp = root, next;
		int ch;
		for (int i = 0; i < word.length(); i++) {
			ch = word.charAt(i) - 'a';
			next = temp.nextNodes[ch];
			if (next == null) {
				next = new TrieNode();
				temp.nextNodes[ch] = next;
			}
			temp = next;
		}
		temp.isEnd = true;
	}

	// Returns if the word is in the trie.
	public boolean search(String word) {
		TrieNode temp = root, next;
		int ch;

		for (int i = 0; i < word.length(); i++) {
			ch = word.charAt(i) - 'a';
			next = temp.nextNodes[ch];
			if (next == null)
				return false;
			temp = next;
		}

		return temp.isEnd;
	}

	// Returns if there is any word in the trie
	// that starts with the given prefix.
	public boolean startsWith(String prefix) {
		TrieNode temp = root, next;
		int ch;

		for (int i = 0; i < prefix.length(); i++) {
			ch = prefix.charAt(i) - 'a';
			next = temp.nextNodes[ch];
			if (next == null)
				return false;
			temp = next;
		}
		return true;
	}

	public static void main(String[] args) {
		Trie t = new Trie();
		t.insert("abb");
		t.insert("c");
		System.out.println(t.search("a"));
		System.out.println(t.startsWith("a"));
	}
}

class TrieNode {
	TrieNode[] nextNodes = new TrieNode[26];
	boolean isEnd;

	TrieNode() {

	}

}


LeetCode208 Implement Trie (Prefix Tree) Java

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

LeetCode 208: Implement Trie (Prefix Tree)

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

Leetcode 208 Implement Trie (Prefix Tree) 实现字典树 (前缀字典树)

实现一个`Trie字典树`,实现 `insert` `search` `startsWith` 方法。

leetcode -- Implement Trie (Prefix Tree) -- 关于字典树,重要

https://leetcode.com/problems/implement-trie-prefix-tree/关于字典树介绍http://www.cnblogs.com/tanky_woo/arc...
  • xyqzki
  • xyqzki
  • 2015年12月24日 16:58
  • 353

LeetCode-208 Implement Trie (Prefix Tree)

题目: mplement a trie with insert, search, and startsWith methods. Note: You may assume that all ...

LeetCode 208 Implement Trie (Prefix Tree)

题目描述Implement a trie with insert, search, and startsWith methods.Note: You may assume that all inpu...

LeetCode Implement Trie (Prefix Tree)

题目 思路 直接前缀树。代码struct TrieNode { char c; struct TrieNode * son[27]; // sons for "abcdefghi...

Leetcode题解 - 208. Implement Trie (Prefix Tree)

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

leetcode笔记:Implement Trie (Prefix Tree)

题目大意很简单,就是实现字典树,其中包括插入、查找和前缀查找三个方法。可以假设所有的输入只包含小写字母a-z。...

[LeetCode 208] Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Note: You may assume that all in...
  • sbitswc
  • sbitswc
  • 2015年08月24日 13:09
  • 568
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Implement Trie (Prefix Tree)
举报原因:
原因补充:

(最多只允许输入30个字)