953. Verifying an Alien Dictionary
Problem Description
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 lexicographically in this alien language.
Example 1:
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
Example 2:
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.
Example 3:
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.
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 20order.length == 26All characters in words[i] and order are English lowercase letters.
Solution
Python Solution
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
# Create a mapping of alien alphabet to normal alphabet
alien_to_normal = {c: i for i, c in enumerate(order)}
# Compare adjacent words
for i in range(len(words) - 1):
word1, word2 = words[i], words[i + 1]
# Compare characters until one word ends
for j in range(min(len(word1), len(word2))):
if word1[j] != word2[j]:
if alien_to_normal[word1[j]] > alien_to_normal[word2[j]]:
return False
break
else:
# If we get here, one word is prefix of another
if len(word1) > len(word2):
return False
return True
Time Complexity: O(M)
Where M is the total number of characters in all words. We need to compare each character at most once.
Space Complexity: O(1)
We only use a constant amount of extra space for the alphabet mapping.
Java Solution
class Solution {
public boolean isAlienSorted(String[] words, String order) {
int[] alienToNormal = new int[26];
for (int i = 0; i < order.length(); i++) {
alienToNormal[order.charAt(i) - 'a'] = i;
}
for (int i = 0; i < words.length - 1; i++) {
String word1 = words[i];
String word2 = words[i + 1];
for (int j = 0; j < Math.min(word1.length(), word2.length()); j++) {
if (word1.charAt(j) != word2.charAt(j)) {
if (alienToNormal[word1.charAt(j) - 'a'] > alienToNormal[word2.charAt(j) - 'a']) {
return false;
}
break;
}
}
// If we get here, one word is prefix of another
if (word1.length() > word2.length()) {
return false;
}
}
return true;
}
}
Time Complexity: O(M)
Where M is the total number of characters in all words. We need to compare each character at most once.
Space Complexity: O(1)
We only use a constant amount of extra space for the alphabet mapping.
C++ Solution
class Solution {
public:
bool isAlienSorted(vector& words, string order) {
vector alienToNormal(26);
for (int i = 0; i < order.length(); i++) {
alienToNormal[order[i] - 'a'] = i;
}
for (int i = 0; i < words.size() - 1; i++) {
string& word1 = words[i];
string& word2 = words[i + 1];
for (int j = 0; j < min(word1.length(), word2.length()); j++) {
if (word1[j] != word2[j]) {
if (alienToNormal[word1[j] - 'a'] > alienToNormal[word2[j] - 'a']) {
return false;
}
break;
}
}
// If we get here, one word is prefix of another
if (word1.length() > word2.length()) {
return false;
}
}
return true;
}
};
Time Complexity: O(M)
Where M is the total number of characters in all words. We need to compare each character at most once.
Space Complexity: O(1)
We only use a constant amount of extra space for the alphabet mapping.
JavaScript Solution
/**
* @param {string[]} words
* @param {string} order
* @return {boolean}
*/
var isAlienSorted = function(words, order) {
const alienToNormal = new Array(26);
for (let i = 0; i < order.length; i++) {
alienToNormal[order.charCodeAt(i) - 97] = i;
}
for (let i = 0; i < words.length - 1; i++) {
const word1 = words[i];
const word2 = words[i + 1];
for (let j = 0; j < Math.min(word1.length, word2.length); j++) {
if (word1[j] !== word2[j]) {
if (alienToNormal[word1.charCodeAt(j) - 97] > alienToNormal[word2.charCodeAt(j) - 97]) {
return false;
}
break;
}
}
// If we get here, one word is prefix of another
if (word1.length > word2.length) {
return false;
}
}
return true;
};
Time Complexity: O(M)
Where M is the total number of characters in all words. We need to compare each character at most once.
Space Complexity: O(1)
We only use a constant amount of extra space for the alphabet mapping.
C# Solution
public class Solution {
public bool IsAlienSorted(string[] words, string order) {
int[] alienToNormal = new int[26];
for (int i = 0; i < order.Length; i++) {
alienToNormal[order[i] - 'a'] = i;
}
for (int i = 0; i < words.Length - 1; i++) {
string word1 = words[i];
string word2 = words[i + 1];
for (int j = 0; j < Math.Min(word1.Length, word2.Length); j++) {
if (word1[j] != word2[j]) {
if (alienToNormal[word1[j] - 'a'] > alienToNormal[word2[j] - 'a']) {
return false;
}
break;
}
}
// If we get here, one word is prefix of another
if (word1.Length > word2.Length) {
return false;
}
}
return true;
}
}
Time Complexity: O(M)
Where M is the total number of characters in all words. We need to compare each character at most once.
Space Complexity: O(1)
We only use a constant amount of extra space for the alphabet mapping.