Friday, April 9, 2021

[Facebook Question][LeetCode] Verifying an Alien Dictionary

Problem: 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:

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).

Constraints:

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


Approach: The approach is straight forward. We take first two words and see if they are sorted using the order string, then we check the next two words see if they are sorted. In the same way we check it for all the pairs. Here is how we can check if the pair of words say word1 and word2 are sorted or not:

  • Get the index 'i'  where word1 and word2 character where the words are different i.e. word1[i] != word2[i].
  • If word1[i]'s index is lesser than the word2[i]'s index in the order string then we can say the words are sorted otherwise not.

That's all!


Implementation in C#:

    public bool IsAlienSorted(string[] words, string order) 

    {

        int[] orderIndices = new int[order.Length];

        for (int i = 0; i < order.Length; ++i)

        {

            orderIndices[order[i] - 'a'] = i;

        }

        for (int i = 0; i < words.Length - 1; ++i)

        {

            string word1 = words[i];

            string word2 = words[i + 1];

            if (word1.Length > word2.Length && word1.StartsWith(word2))

            {

                return false;

            }

            int j = 0;            

            while (j < word1.Length && word1[j] == word2[j])

            {

                ++j;

            }

            if (j != word1.Length)

            {

                if (orderIndices[word1[j] - 'a'] > orderIndices[word2[j] - 'a'])

                {

                    return false;

                }

            }

        }

        return true;

    }


Complexity: O(n) where n are the number of characters in all the words in input words array.

No comments:

Post a Comment