Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Fixed topo sort solution in Clear category for Determine the Order by nickie
def topological_sort(fwd, rev):
L = []
S = set(u for u, incoming in rev.items() if not incoming)
while S:
u = min(S)
S.remove(u)
L.append(u)
for v in fwd[u]:
rev[v].remove(u)
if not rev[v]:
S.add(v)
return L
def checkio(data):
fwd = {}
rev = {}
for word in data:
n = len(word)
for i in range(n):
if word[i] not in fwd:
fwd[word[i]] = set()
rev[word[i]] = set()
for i in range(n-1):
if word[i+1] != word[i]:
fwd[word[i]].add(word[i+1])
rev[word[i+1]].add(word[i])
t = topological_sort(fwd, rev)
answer = "".join("".join(sorted(l)) for l in t)
return answer
May 5, 2014