LeetCode 953. Verifying an Alien Dictionary
Question
In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order
. The order
of the alphabet is some permutation of lowercase letters.
Given a sequence of words
written in the alien language, and the order
of the alphabet, return true
if and only if the given words
are sorted lexicographicaly in this alien language.
Solution
Actually, the idea is pretty straight forward. We have to see the position of each letter in adjant string and to compare. If there exists less or greater, we can return true or false. Otherwise, we go to the next letter. For the optimization, we can use count sort(mapping first) instead of using Java builtin indexOf
.
Code
1 | // time:O(# of words * max(word)) |