public Optional < String > get ( String input ) { TrieNode currentNode = root ; int i = 0 ; for ( char c : input . toCharArray ( ) ) { TrieNode nextNode = currentNode . getChildren ( ) . get ( c ) ; if ( nextNode != null ) { i ++ ; currentNode = nextNode ; } else { if ( i > 0 && currentNode . isLeaf ( ) ) { return Optional . of ( input . substring ( 0 , i ) ) ; } } } if ( i > 0 && currentNode . isLeaf ( ) ) { return Optional . of ( input . substring ( 0 , i ) ) ; } return Optional . empty ( ) ; }
Return the longest matching prefix of the input string that was added to the trie .