@Test public void testLongestPrefix2() { trie = new Trie(new String[] { "are", "area", "base", "cat", "cater", "basement" }); Assert.assertEquals("cater", trie.longestPrefix("caterer")); Assert.assertEquals("basement", trie.longestPrefix("basement")); Assert.assertEquals("are", trie.longestPrefix("are")); Assert.assertEquals("are", trie.longestPrefix("arex")); Assert.assertEquals("base", trie.longestPrefix("basemexz")); Assert.assertNull(trie.longestPrefix("xyz")); Assert.assertNull(trie.longestPrefix("")); Assert.assertNull(trie.longestPrefix(null)); Assert.assertNull(trie.longestPrefix(" ")); }
public String longestPrefix(String inputWord) { if (inputWord == null || inputWord.isEmpty()) { return null; } Node node = root; Node child = null; char ch = '\u0000'; final int len = inputWord.length(); StringBuilder runningPrefix = new StringBuilder(); String longestPrefix = null; for (int idx = 0; idx < len; idx++) { ch = inputWord.charAt(idx); child = node.getChild(ch); if (node.isEndOfWord) { longestPrefix = runningPrefix.toString(); } if (child != null) { runningPrefix.append(ch); node = child; } else { break; } } if (node.isEndOfWord && node.getChild(ch) == null && ch == node.value) { longestPrefix = runningPrefix.toString(); } return longestPrefix; }
Trie { public String longestPrefix(String inputWord) { if (inputWord == null || inputWord.isEmpty()) { return null; } Node node = root; Node child = null; char ch = '\u0000'; final int len = inputWord.length(); StringBuilder runningPrefix = new StringBuilder(); String longestPrefix = null; for (int idx = 0; idx < len; idx++) { ch = inputWord.charAt(idx); child = node.getChild(ch); if (node.isEndOfWord) { longestPrefix = runningPrefix.toString(); } if (child != null) { runningPrefix.append(ch); node = child; } else { break; } } if (node.isEndOfWord && node.getChild(ch) == null && ch == node.value) { longestPrefix = runningPrefix.toString(); } return longestPrefix; } }
Trie { public String longestPrefix(String inputWord) { if (inputWord == null || inputWord.isEmpty()) { return null; } Node node = root; Node child = null; char ch = '\u0000'; final int len = inputWord.length(); StringBuilder runningPrefix = new StringBuilder(); String longestPrefix = null; for (int idx = 0; idx < len; idx++) { ch = inputWord.charAt(idx); child = node.getChild(ch); if (node.isEndOfWord) { longestPrefix = runningPrefix.toString(); } if (child != null) { runningPrefix.append(ch); node = child; } else { break; } } if (node.isEndOfWord && node.getChild(ch) == null && ch == node.value) { longestPrefix = runningPrefix.toString(); } return longestPrefix; } Trie(String[] words); }
Trie { public String longestPrefix(String inputWord) { if (inputWord == null || inputWord.isEmpty()) { return null; } Node node = root; Node child = null; char ch = '\u0000'; final int len = inputWord.length(); StringBuilder runningPrefix = new StringBuilder(); String longestPrefix = null; for (int idx = 0; idx < len; idx++) { ch = inputWord.charAt(idx); child = node.getChild(ch); if (node.isEndOfWord) { longestPrefix = runningPrefix.toString(); } if (child != null) { runningPrefix.append(ch); node = child; } else { break; } } if (node.isEndOfWord && node.getChild(ch) == null && ch == node.value) { longestPrefix = runningPrefix.toString(); } return longestPrefix; } Trie(String[] words); String longestPrefix(String inputWord); }
Trie { public String longestPrefix(String inputWord) { if (inputWord == null || inputWord.isEmpty()) { return null; } Node node = root; Node child = null; char ch = '\u0000'; final int len = inputWord.length(); StringBuilder runningPrefix = new StringBuilder(); String longestPrefix = null; for (int idx = 0; idx < len; idx++) { ch = inputWord.charAt(idx); child = node.getChild(ch); if (node.isEndOfWord) { longestPrefix = runningPrefix.toString(); } if (child != null) { runningPrefix.append(ch); node = child; } else { break; } } if (node.isEndOfWord && node.getChild(ch) == null && ch == node.value) { longestPrefix = runningPrefix.toString(); } return longestPrefix; } Trie(String[] words); String longestPrefix(String inputWord);  }