String-mainipulation9

题目描述[原题描述][https://leetcode-cn.com/problems/implement-trie-prefix-tree/]

实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。

示例:

Trie trie = new Trie();

trie.insert(“apple”);
trie.search(“apple”); // 返回 true
trie.search(“app”); // 返回 false
trie.startsWith(“app”); // 返回 true
trie.insert(“app”);
trie.search(“app”); // 返回 true

说明:

你可以假设所有的输入都是由小写字母 a-z构成的。
保证所有输入均为非空字符串。

算法描述

​ 1.首先需要定义树中每个结点的结构,包含26个儿子的指针和一个bool变量代表是否是一个单词的结尾。

​ 2.插入时沿着根节点向下寻找,如果不存在该路径,则通过创建结点来生成路径。

​ 3.查询和查询前缀时沿着路径查找即可。

C++代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Trie {
public:
/** Initialize your data structure here. */
struct node
{
int next[26];
int book;
}tree[100020];
int len=0;
int root=0;
Trie() {
memset(tree,0,sizeof(tree));
root=0;
len=1;

}

/** Inserts a word into the trie. */
void insert(string word) {
int l=word.size();
int now=0;
for(int i=0;i<l;i++)
if(tree[now].next[word[i]-'a'])
now=tree[now].next[word[i]-'a'];
else
{
tree[now].next[word[i]-'a']=len++;
now=tree[now].next[word[i]-'a'];
}
tree[now].book=1;

}

/** Returns if the word is in the trie. */
bool search(string word) {
int l=word.size();
int now=root;
for(int i=0;i<l;i++)
if(tree[now].next[word[i]-'a'])
now=tree[now].next[word[i]-'a'];
else
{
return false;
}
if(tree[now].book)return true;
else return false;
}

/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
int l=prefix.size();
int now=root;
for(int i=0;i<l;i++)
if(tree[now].next[prefix[i]-'a'])
now=tree[now].next[prefix[i]-'a'];
else
{
return false;
}
return true;
}
};

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Trie {
TrieNode root;
/** Initialize your data structure here. */
public Trie() {
root = new TrieNode();
}

/** Inserts a word into the trie. */
public void insert(String word) {
TrieNode node = root;
for(int i =0;i<word.length();i++){
char currentChar = word.charAt(i);
if(!node.containsKey(currentChar)){
node.put(currentChar,new TrieNode());
}
node = node.get(currentChar);
}
node.setEnd();
}

private TrieNode searchPrefix(String word){
TrieNode node = root;
for(int i=0;i<word.length();i++){
char curLetter = word.charAt(i);
if(node.containsKey(curLetter)){
node = node.get(curLetter);
}else{
return null;
}
}
return node;
}

/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode node = searchPrefix(word);
return node!=null&&node.isEnd();
}

/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode node = searchPrefix(prefix);
return node !=null;
}
}

class TrieNode{
private TrieNode[] links;
private final int R = 26;
private boolean isEnd;

public TrieNode(){
links = new TrieNode[R];
}

public boolean containsKey(char ch){
return links[ch-'a'] != null;
}

public TrieNode get(char ch){
return links[ch-'a'];
}

public void put(char ch,TrieNode node){
links[ch-'a'] = node;
}

public void setEnd(){
isEnd = true;
}

public boolean isEnd(){
return isEnd;
}
}