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.
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
All characters in words[i] and order are English lowercase letters.
Solutions (Click to expand)
In the ASCII table the lowercase letters of the alphabet are sorted a-z
with character codes 97-122
. We can replicate the same with the alien alphabet by creating a table that will store the codes for each letter in the alien alphbet depending on their order. We can then use that tabel to check if the string are lexigraphically sorted.
To create a table we can use a hash map of size 26
to represent all letter in the alphabet. Their key will be the character and their place will be their value. Since we can cast our characters to integers, we can also use an array of size 26
Where the index is the character code in the original alphabet, the content at i
is its place in the alien alphabet
alien = "hlabcdefgijkmnopqrstuvwxyz"
map = {
"a": 2,
"b": 3,
"c": 4,
"d": 5,
...
}
map = [2 3 4 5 ...]
// with an array, `map['a']` is the place of 'a' in the alien alphabet.
Now to check the ordering of the strings we would need to check the characters of one string with the character of the next string.
To check lexicographic ordering:
-
The string's length must be equal to or less than the length of the next string
-
Every
s1[i]
must be less than or equal tos2[i]
-
If
s1
is a prefix ofs2
ands2
is longer,s1
is sorted
Time: O(N * M)
Space: O(26)