import heapq

def alien_order(words):
    adj = {char: set() for word in words for char in word}
    indegree = dict.fromkeys(adj, 0)
    for first, second in zip(words, words[1:]):
        for a, b in zip(first, second):
            if a != b:
                if b not in adj[a]:
                    adj[a].add(b)
                    indegree[b] += 1
                break
        else:
            if len(first) > len(second):
                return ''
    heap = [char for char in adj if indegree[char] == 0]
    heapq.heapify(heap)
    output = []
    while heap:
        char = heapq.heappop(heap)
        output.append(char)
        for neighbor in adj[char]:
            indegree[neighbor] -= 1
            if indegree[neighbor] == 0:
                heapq.heappush(heap, neighbor)
    return ''.join(output) if len(output) == len(adj) else ''
