{x}
blog image

Alien Dictionary

Solution Explanation for Alien Dictionary

This problem involves finding a lexicographical order of characters based on a list of alien words. The words are lexicographically sorted according to the alien language's rules. The solution uses topological sort to determine a valid ordering. If no valid ordering exists, it indicates a contradiction in the given words and returns an empty string.

Approach

  1. Build the Graph: Create a directed graph where each node represents a character (a-z). A directed edge from character u to character v implies that u comes before v in the alien language's order. This is determined by comparing consecutive words in the input list. If word1 and word2 are consecutive and the first difference is word1[i] = u and word2[i] = v, then we add a directed edge from u to v.

  2. Detect Cycles: A cycle in the graph means there is a contradiction in the given words (e.g., "ab" and "ba" cannot be lexicographically sorted). If a cycle exists, return an empty string.

  3. Topological Sort: Perform topological sort on the graph. Topological sort arranges nodes in a graph such that for every directed edge from node u to node v, u appears before v in the ordering.

  4. Construct the Result: The result of the topological sort represents a valid ordering of characters in the alien language. Convert the sorted character indices back to characters to construct the result string.

Time Complexity Analysis

  • Building the Graph: The time complexity of building the graph is O(C*M), where C is the number of characters in the alien language, and M is the total number of characters across all words (sum of lengths of all words). This is because we iterate through all words and compare consecutive words, character by character.

  • Cycle Detection (implicitly done during topological sort): The time complexity of detecting cycles using topological sort is also O(V + E), where V is the number of vertices (characters), and E is the number of edges in the graph. Since V is at most 26 (English alphabet) and E is at most 26*26 (in the worst case, every character is connected to every other character), the complexity is considered O(1) in practical terms as it is bounded by a constant.

  • Topological Sort: The time complexity of the topological sort using Kahn's algorithm (as used in the code) is O(V + E), which is again considered O(1) in the context of this problem due to the constant bounds.

Therefore, the overall time complexity of the solution is dominated by graph construction and is O(C * M).

Space Complexity Analysis

  • The space complexity is dominated by the graph representation, which is a 26x26 adjacency matrix, using O(1) space.
  • The queue used for topological sort also takes O(V) = O(1) space.
  • Therefore, the overall space complexity is O(1).

Code Explanation (Python)

The Python code implements the approach described above. It first initializes the graph (g) and a set of seen characters (s). Then it iterates through the words to build the graph and detect contradictions. Finally, it performs topological sort using Kahn's algorithm to get the sorted order.

class Solution:
    def alienOrder(self, words: List[str]) -> str:
        # ... (Graph building, cycle detection, and topological sort as explained above)

The Java and C++ codes follow the same logic with slight syntax differences. The key data structures and algorithms remain the same. They all maintain a graph, detect cycles using topological sort, and handle the cases where no valid order exists.