No Prefix Set

  • + 0 comments

    Tried using .sort() which could have been an effective O(n) solution as you only have to compare neighbouring words. Unfortunately this violates the "tested first" condition due to the reordering of words:

    def noPrefix_fail(words: list) -> None:
        words.sort()    # sort leixcographically
        for w1, w2 in zip(words, words[1:]): # sliding window of word pairs
            if w2.startswith(w1):
                print("BAD SET")
                print(w1)   # incorrect due to .sort()
                return
        print("GOOD SET")