2025-08-01 16:16:55 +0000 UTC

Verifying an Alien Dictionary

Code

class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        pos = {}
        for i, char in enumerate(order):
            pos[char] = i
        for i in range(len(words) - 1):
            cur, nxt = words[i], words[i + 1]
            cur_length, nxt_length = len(cur), len(nxt)
            for j in range(cur_length):
                if j >= nxt_length:
                    return False
                cur_char, nxt_char = cur[j], nxt[j]
                if cur_char == nxt_char:
                    continue
                if pos[cur_char] > pos[nxt_char]: 
                    return False
                break
        return True