Straightforward python, no trie


  • 0
    A
        def replaceWords(self, dict, sentence):
            dict = sorted(dict, key=lambda x: len(x))
            words = []
            for w in sentence.split():
                for k in dict:
                    if w.startswith(k):
                        words.append(k)
                        break
                else:
                    words.append(w)
            
            return ' '.join(words)

Log in to reply
 

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