profile picture

    Alien Dictionary

    Hard

    Problem Description

    There is a new alien language that uses the English alphabet, but the letter order is unknown.

    You are given a list of words sorted lexicographically by that alien language. Derive a valid character order.

    Return an empty string if:

    • the input is invalid (prefix conflict), or
    • the graph has a cycle.

    Examples

    Example 1:

    Input: words = ["wrt","wrf","er","ett","rftt"]
    Output: "wertf"
    

    Example 2:

    Input: words = ["z","x","z"]
    Output: ""
    

    Constraints

    • 1 <= words.length <= 100
    • 1 <= words[i].length <= 100
    • All words contain lowercase English letters.

    Hint

    Build precedence edges from the first differing character of adjacent words, then topologically sort.

    Solution

    AI Code Assistant

    Test Results

    Run tests to see the overall score, band, and section breakdown.

    Public Tests

    Detailed feedback for visible cases.

    Public test details will appear here after you run your solution.

    Hidden Tests

    Summary only, with no hidden inputs or outputs.

    No hidden test results yet.

    Performance

    Execution timing and pass/fail status.

    No performance results yet.

    AI Interviewer