Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using a trie solution in Speedy category for The End of Other by nickie
class Trie:
def __init__(self):
self.word = False
self.next = {}
def add(self, word):
"""
Adds words in the trie right-to-left; returns something true if two
words are found, such that the one is a suffix of the other.
"""
t = self
for c in reversed(word):
if t.word: return True # some other word is a suffix of this
if c not in t.next: t.next[c] = Trie()
t = t.next[c]
t.word = True
return t.next # this word may be a suffix of some other
def checkio(words):
t = Trie()
return any(t.add(w) for w in words)
May 13, 2014
Comments: