@Test public void test_findLongestWord_1() throws Exception{ Trie t = trieWithWords("hello", "helloworld", "hi", "howsgoing", "hell", "helloworld2", "world"); String text = "The helloworld."; StringBuilder b = new StringBuilder(); int i = t.findLongestWord(text, 0, text.length(), b); Assert.assertEquals(4, i); Assert.assertEquals("helloworld", b.toString()); }
@Override public int findLongestWord(CharSequence chars, int start, int end, StringBuilder word) { for(int i = start; i < end; i++){ Iterator<String> it = commonPrefixSearch(chars.subSequence(i, end).toString()).iterator(); String last = null; while(it.hasNext()){ last = it.next(); } if(last != null){ if(word != null) word.append(last); return i; } } return -1; }
AbstractTrie implements Trie { @Override public int findLongestWord(CharSequence chars, int start, int end, StringBuilder word) { for(int i = start; i < end; i++){ Iterator<String> it = commonPrefixSearch(chars.subSequence(i, end).toString()).iterator(); String last = null; while(it.hasNext()){ last = it.next(); } if(last != null){ if(word != null) word.append(last); return i; } } return -1; } }
AbstractTrie implements Trie { @Override public int findLongestWord(CharSequence chars, int start, int end, StringBuilder word) { for(int i = start; i < end; i++){ Iterator<String> it = commonPrefixSearch(chars.subSequence(i, end).toString()).iterator(); String last = null; while(it.hasNext()){ last = it.next(); } if(last != null){ if(word != null) word.append(last); return i; } } return -1; }  }
AbstractTrie implements Trie { @Override public int findLongestWord(CharSequence chars, int start, int end, StringBuilder word) { for(int i = start; i < end; i++){ Iterator<String> it = commonPrefixSearch(chars.subSequence(i, end).toString()).iterator(); String last = null; while(it.hasNext()){ last = it.next(); } if(last != null){ if(word != null) word.append(last); return i; } } return -1; }  @Override int findWord(CharSequence chars, int start, int end, StringBuilder word); @Override int findShortestWord(CharSequence chars, int start, int end, StringBuilder word); @Override int findLongestWord(CharSequence chars, int start, int end, StringBuilder word); @Override void dump(Writer writer); @Override void trimToSize(); @Override void freeze(); @Override void insert(String word); }
AbstractTrie implements Trie { @Override public int findLongestWord(CharSequence chars, int start, int end, StringBuilder word) { for(int i = start; i < end; i++){ Iterator<String> it = commonPrefixSearch(chars.subSequence(i, end).toString()).iterator(); String last = null; while(it.hasNext()){ last = it.next(); } if(last != null){ if(word != null) word.append(last); return i; } } return -1; }  @Override int findWord(CharSequence chars, int start, int end, StringBuilder word); @Override int findShortestWord(CharSequence chars, int start, int end, StringBuilder word); @Override int findLongestWord(CharSequence chars, int start, int end, StringBuilder word); @Override void dump(Writer writer); @Override void trimToSize(); @Override void freeze(); @Override void insert(String word);  }