@Test public void testLongestPrefix1() { trie = new Trie(new String[] { "abc", "abcd", "abcdde" }); Assert.assertEquals("abcd", trie.longestPrefix("abcdd")); }
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);  }