953. Verifying an Alien Dictionary LeetCode Solution

In this guide, you will get 953. Verifying an Alien Dictionary LeetCode Solution with the best time and space complexity. The solution to Verifying an Alien Dictionary problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Verifying an Alien Dictionary solution in C++
  4. Verifying an Alien Dictionary solution in Java
  5. Verifying an Alien Dictionary solution in Python
  6. Additional Resources
953. Verifying an Alien Dictionary LeetCode Solution image

Problem Statement of Verifying an Alien Dictionary

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 (More info).

See also  1001. Grid Illumination LeetCode Solution

Constraints:

1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
All characters in words[i] and order are English lowercase letters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(26) = O(1)

953. Verifying an Alien Dictionary LeetCode Solution in C++

class Solution {
 public:
  bool isAlienSorted(vector<string>& words, const string& order) {
    vector<char> map(26);  // e.g. order = "bca" -> map = ['c', 'a', 'b']

    for (int i = 0; i < 26; ++i)
      map[order[i] - 'a'] = i + 'a';

    for (string& word : words)
      for (char& c : word)
        c = map[c - 'a'];

    return is_sorted(words.begin(), words.end());
  }
};
/* code provided by PROGIEZ */

953. Verifying an Alien Dictionary LeetCode Solution in Java

class Solution {
  public boolean isAlienSorted(String[] words, String order) {
    char[] map = new char[26]; // e.g. order = "bca" -> map = ['c', 'a', 'b']

    for (int i = 0; i < 26; ++i)
      map[order.charAt(i) - 'a'] = (char) (i + 'a');

    for (int i = 0; i + 1 < words.length; ++i)
      if (bigger(words[i], words[i + 1], map))
        return false;

    return true;
  }

  private boolean bigger(final String s1, final String s2, final char[] map) {
    for (int i = 0; i < s1.length() && i < s2.length(); ++i)
      if (s1.charAt(i) != s2.charAt(i))
        return map[s1.charAt(i) - 'a'] > map[s2.charAt(i) - 'a'];
    return s1.length() > s2.length();
  }
}
// code provided by PROGIEZ

953. Verifying an Alien Dictionary LeetCode Solution in Python

class Solution:
  def isAlienSorted(self, words: list[str], order: str) -> bool:
    dict = {c: i for i, c in enumerate(order)}
    words = [[dict[c] for c in word] for word in words]
    return all(w1 <= w2 for w1, w2 in zip(words, words[1:]))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.