标签:
Implement a trie with insert
, search
, and startsWith
methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z
solution:
class TrieNode { // Initialize your data structure here. boolean isEnd; TrieNode[] sons; char value; public TrieNode() { isEnd = false; sons = new TrieNode[26]; } } 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 = root; for (int i = 0; i < word.length(); i++) { int pos = word.charAt(i) - ‘a‘; if (p.sons[pos] == null) { p.sons[pos] = new TrieNode(); p.sons[pos].value = word.charAt(i); } p = p.sons[pos]; } p.isEnd = true; } // Returns if the word is in the trie. public boolean search(String word) { if (word == null || word.length() == 0) return false; TrieNode p = root; for (int i = 0; i < word.length(); i++) { int pos = word.charAt(i) - ‘a‘; if (p.sons[pos] == null) return false; p = p.sons[pos]; } return p.isEnd; } // 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; TrieNode p = root; for (int i = 0; i < prefix.length(); i++) { int pos = prefix.charAt(i) - ‘a‘; if (p.sons[pos] == null) return false; p = p.sons[pos]; } return true; } } // Your Trie object will be instantiated and called as such: // Trie trie = new Trie(); // trie.insert("somestring"); // trie.search("key");
Add and Search Word
Design a data structure that supports the following two operations:
void addWord(word) bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z
or .
. A .
means it can represent any one letter.
For example:
addWord("bad") addWord("dad") addWord("mad") search("pad") -> false search("bad") -> true search(".ad") -> true search("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z
.
solution:
public class WordDictionary { private Tode root; public WordDictionary() { root = new Tode(); } // Adds a word into the data structure. public void addWord(String word) { if(word == null || word.length() == 0) return ; Tode p = root; for(int i=0; i<word.length(); i++){ int pos = word.charAt(i) - ‘a‘; if(p.sons[pos] == null){ p.sons[pos] = new Tode(); p.sons[pos].value = word.charAt(i); } p = p.sons[pos]; } p.isEnd = true; } // Returns if the word is in the data structure. A word could // contain the dot character ‘.‘ to represent any one letter. public boolean search(String word) { if(word == null || word.length() == 0) return false; char[] arr = word.toCharArray(); int index = 0; Tode p = root; return goSearch(arr, index, p); } private boolean goSearch(char[] arr, int index, Tode p) { if(index == arr.length) return p.isEnd; if(arr[index] == ‘.‘){ for(Tode n : p.sons){ if(n != null && goSearch(arr, index+1, n)) return true; } return false; }else{ int pos = arr[index] - ‘a‘; if(p.sons[pos] != null) return goSearch(arr, index+1, p.sons[pos]); else return false; } } class Tode{ boolean isEnd; Tode[] sons; char value; public Tode(){ isEnd = false; sons = new Tode[26]; } } } // Your WordDictionary object will be instantiated and called as such: // WordDictionary wordDictionary = new WordDictionary(); // wordDictionary.addWord("word"); // wordDictionary.search("pattern");
(Data structure)Implement Trie And Add and Search Word
标签:
原文地址:http://www.cnblogs.com/axolotl/p/4608899.html