模板题目:Trie( 720 Longest Word in Dictionary)
Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order.
If there is no answer, return the empty string.
一看这题目描述 就是trie啊。
LC208 Implement trie
这道题目就是典型的实现字典树。
宏观的思路:
要实现两个类:一个Trie 一个TrieNode
类TrieNode里面 有两个属性:26个子节点组成的数组以及isWord属性 没有任何方法 就和LinkedList Node一样
类Trie里面 有一个属性 TrieNode root. 方法有insert(String word), search(String word)->search(cur, word, index) startsWith(String prefix)
所以trie的类的实现如下:
class Trie {
TrieNode root; //attribute, which can show a trie's identity
/** Initialize your data structure here. */
public Trie() { //constuctor, which used to create a new trie instance
root = new TrieNode();
}
//we need to implement three methods: insert, search and startsWith
/** Inserts a word into the trie. */
public void insert(String word) {
TrieNode cur = root;
for(char c: word.toCharArray()){
if(cur.children[c-'a'] == null){
cur.children[c-'a'] = new TrieNode();
}
cur = cur.children[c-'a'];
}
cur.isWord = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
return search(root, word, 0);
}
private boolean search(TrieNode cur, String word, int index){
if(index == word.length()){ //when inddex pointer reach the end of word
return cur.isWord;//check current node has attribute isWord is true or not.
}
char c = word.charAt(index);
TrieNode temp = cur.children[c-'a']; //until you've search every c, which means temp can't be null in every level, if temp == null, then it means trie stoped here but word is not finished
return temp != null && search(temp, word, index+1);
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) { //check if a given prefix is the prefix of current trie
TrieNode cur = root;
for(char c: prefix.toCharArray()){
if(cur.children[c-'a'] == null){
return false;
}
cur = cur.children[c-'a'];
}
return true;
}
}
//Trie Node
class TrieNode {
TrieNode[] children; //children attribute, which is a trieNode array
boolean isWord; //isWord attribute, check if current node is a word or not
public TrieNode(){ //constructor, we don;t have to pass anything for this node,
children = new TrieNode[26]; //initialize children and isWord attribute
isWord = false;
}
}
但是具体做题的时候 我们不一定要把所有的这写规规整整的写下来。
就以720为例子:
DFS+Trie
class Solution {
public String longestWord(String[] words) {
TrieNode root = new TrieNode ();
root.word = "-";
for (String word : words)
root.insert (word);
return dfs (root, "");
}
String dfs (TrieNode node, String accum) {
if (node == null || node.word.length () == 0)
return accum;
String res = "";
if (!node.word.equals ("-"))
accum = node.word;
for (TrieNode child : node.links) {
String curRes = dfs (child, accum);
if (curRes.length () > res.length () || (curRes.length () == res.length () && curRes.compareTo (res) < 0))
res = curRes;
}
return res;
}
/* Hand write this class every time you need to so you can remember well */
static class TrieNode {
String word = "";
TrieNode[] links = new TrieNode[26];
void insert (String s) {
char[] chs = s.toCharArray ();
TrieNode curNode = this;
for (int i = 0; i < chs.length; i++) {
int index = chs[i] - 'a';
if (curNode.links[index] == null)
curNode.links[index] = new TrieNode ();
curNode = curNode.links[index];
}
curNode.word = s;
}
}
}

浙公网安备 33010602011771号