442. Implement Trie (Prefix Tree)
1 class TrieNode { 2 public boolean isWord; 3 public TrieNode[] children; 4 5 public TrieNode() { 6 isWord = false; 7 children = new TrieNode[26]; 8 } 9 10 } 11 12 public class Trie { 13 private TrieNode root; 14 15 public Trie() { 16 // do intialization if necessary 17 root = new TrieNode(); 18 } 19 20 /* 21 * @param word: a word 22 * @return: nothing 23 */ 24 public void insert(String word) { 25 // write your code here 26 if (word == null || word.length() == 0) { 27 return; 28 } 29 TrieNode p = root; 30 for (int i = 0; i < word.length(); i++) { 31 int index = word.charAt(i) - 'a'; 32 if (p.children[index] == null) { 33 p.children[index] = new TrieNode(); 34 } 35 p = p.children[index]; 36 } 37 p.isWord = true; 38 } 39 40 public TrieNode find(String prefix) { 41 if (prefix == null || prefix.length() == 0) { 42 return null; 43 } 44 TrieNode p = root; 45 for (int i = 0; i < prefix.length(); i++) { 46 int index = prefix.charAt(i) - 'a'; 47 if (p.children[index] == null) { 48 return null; 49 } 50 p = p.children[index]; 51 } 52 return p; 53 } 54 55 /* 56 * @param word: A string 57 * @return: if the word is in the trie. 58 */ 59 public boolean search(String word) { 60 // write your code here 61 TrieNode p = find(word); 62 return p != null && p.isWord; 63 } 64 65 /* 66 * @param prefix: A string 67 * @return: if there is any word in the trie that starts with the given prefix. 68 */ 69 public boolean startsWith(String prefix) { 70 // write your code here 71 return find(prefix) != null; 72 } 73 }
473. Add and Search Word - Data structure design
1 class TrieNode { 2 public boolean isWord; 3 public char c; 4 public Map<Character, TrieNode> children; 5 6 public TrieNode() { 7 children = new HashMap<>(); 8 } 9 10 public TrieNode(char c) { 11 this.c = c; 12 children = new HashMap<>(); 13 } 14 } 15 16 17 public class WordDictionary { 18 /* 19 * @param word: Adds a word into the data structure. 20 * @return: nothing 21 */ 22 TrieNode root = new TrieNode(); 23 24 public void addWord(String word) { 25 // write your code here 26 if (word == null || word.length() == 0) { 27 return; 28 } 29 30 TrieNode p = root; 31 for (int i = 0; i < word.length(); i++) { 32 char c = word.charAt(i); 33 if (p.children.get(c) == null) { 34 TrieNode child = new TrieNode(); 35 p.children.put(c, child); 36 } 37 38 p = p.children.get(c); 39 } 40 p.isWord = true; 41 } 42 43 /* 44 * @param word: A word could contain the dot character '.' to represent any one letter. 45 * @return: if the word is in the data structure. 46 */ 47 public boolean search(String word) { 48 // write your code here 49 if (word == null || word.length() == 0) { 50 return false; 51 } 52 TrieNode p = root; 53 54 for (int i = 0; i < word.length(); i++) { 55 char c = word.charAt(i); 56 if (c != '.') { 57 if (p.children.get(c) == null) { 58 return false; 59 } 60 p = p.children.get(c); 61 } else { 62 for (Map.Entry<Character, TrieNode> entry : p.children.entrySet()) { 63 if (search(word.substring(0, i) + entry.getKey() + word.substring(i + 1, word.length()))) { 64 return true; 65 } 66 } 67 return false; 68 } 69 70 } 71 72 return p.isWord; 73 } 74 }
132. Word Search II
1 class TrieNode { 2 String word; 3 Map<Character, TrieNode> children; 4 5 public TrieNode() { 6 children = new HashMap<>(); 7 } 8 } 9 10 class TrieTree { 11 TrieNode root; 12 13 public TrieTree(TrieNode node) { 14 this.root = node; 15 } 16 17 public void insert(String word) { 18 if (word == null || word.length() == 0) { 19 return; 20 } 21 TrieNode p = root; 22 for (int i = 0; i < word.length(); i++) { 23 char ch = word.charAt(i); 24 if (!p.children.containsKey(ch)) { 25 p.children.put(ch, new TrieNode()); 26 } 27 p = p.children.get(ch); 28 } 29 p.word = word; 30 } 31 } 32 33 public class Solution { 34 private int[] dx = {0, 1, 0, -1}; 35 private int[] dy = {1, 0, -1, 0}; 36 37 /** 38 * @param board: A list of lists of character 39 * @param words: A list of string 40 * @return: A list of string 41 */ 42 public List<String> wordSearchII(char[][] board, List<String> words) { 43 // write your code here 44 if (words == null || words.size() == 0) { 45 return new ArrayList<>(); 46 } 47 Set<String> res = new HashSet<>(); 48 TrieTree tree = new TrieTree(new TrieNode()); 49 for (String word : words) { 50 tree.insert(word); 51 } 52 for (int i = 0; i < board.length; i++) { 53 for (int j = 0; j < board[i].length; j++) { 54 dfs(board, i, j, res, tree.root); 55 } 56 } 57 return new ArrayList<>(res); 58 } 59 60 public void dfs(char[][] board, int x, int y, Set<String> res, TrieNode node) { 61 TrieNode child = node.children.get(board[x][y]); 62 if (child == null) { 63 return; 64 } 65 if (child.word != null) { 66 if (!res.contains(child.word)) { 67 res.add(child.word); 68 } 69 } 70 71 char tmp = board[x][y]; 72 board[x][y] = 0; 73 for (int i = 0; i < dx.length; i++) { 74 int nxtDx = x + dx[i]; 75 int nxtDy = y + dy[i]; 76 if (!isValid(board, nxtDx, nxtDy)) { 77 continue; 78 } 79 dfs(board, nxtDx, nxtDy, res, child); 80 } 81 board[x][y] = tmp; 82 } 83 84 public boolean isValid(char[][] board, int x, int y) { 85 if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) { 86 return false; 87 } 88 return board[x][y] != 0; 89 } 90 }