class Trie {
Trie[] chs= new Trie[26];
int cnt = 0;
public Trie() {
}
public void insert(String word) {
Trie root = this;
for(char ch : word.toCharArray()){
if(root.chs[ch - 'a'] == null){
root.chs[ch - 'a'] = new Trie();
}
root = root.chs[ch - 'a'];
}
root.cnt ++;
}
public boolean search(String word) {
Trie root = this;
for(char ch : word.toCharArray()){
if(root.chs[ch - 'a'] == null)
return false;
root = root.chs[ch - 'a'];
}
return root.cnt != 0;
}
public boolean startsWith(String prefix) {
Trie root = this;
for(char ch : prefix.toCharArray()){
if(root.chs[ch - 'a'] == null)
return false;
root = root.chs[ch - 'a'];
}
return true;
}
}
标签:ch,return,前缀,Trie,208,chs,root,public
From: https://www.cnblogs.com/ganyq/p/18109126