Can this be done using a trie?


  • 0
    R

    I don't have an answer yet using a Trie (still working on it..), but if anyone has come up with one. Looking for suggestions or tips.


  • 0
    L

    @rohan5 Trie will work like this only when you make a trie tree from the substring rather the string itself.

    The idea is something like:

    TrieNode root = ///"lo";
    String s1 = "hello";
    
    for(int i=0;i<s1.length();i++) {
      if(root.matches(s1.substring(i)) {
         return i;
      }
    }
    return -1;
    
    

    It may work but KMP is a better approach IMHO.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.