# Check if words are sorted according to new order of alphabets

Given a sequence of Words and the Order of the alphabet. The order of the alphabet is some permutation of lowercase letters. The task is to check whether the given words are sorted lexicographically according to order of alphabets. Return “True” if it is, otherwise “False”.

**Examples:**

Input :Words = [“hello”, “leetcode”], Order = “habcldefgijkmnopqrstuvwxyz”

Output :true

Input :Words = [“word”, “world”, “row”], Order = “abcworldefghijkmnpqstuvxyz”

Output :false

Explanation :As ‘d’ comes after ‘l’ in Order, thus words[0] > words[1], hence the sequence is unsorted.

**Approach :** The words are sorted lexicographically if and only if adjacent words are sorted. This is because order is transitive i:e if a <= b and b <= c, implies a <= c.

So our goal it to check whether all adjacent words a and b have a <= b.

To check whether for two adjacent words a and b, a <= b holds we can find their first difference. For example, "seen" and "scene" have a first difference of e and c. After this, we compare these characters to the index in order.

We have to deal with the blank character effectively. If for example, we are comparing "add" to "addition", this is a first difference of (NULL) vs "i".

**Below is the implementation of above approach :**

## Python3

`# Function to check whether Words are sorted in given Order ` `def` `isAlienSorted(Words, Order): ` ` ` `Order_index ` `=` `{c: i ` `for` `i, c ` `in` `enumerate` `(Order)} ` ` ` ` ` `for` `i ` `in` `range` `(` `len` `(Words) ` `-` `1` `): ` ` ` `word1 ` `=` `Words[i] ` ` ` `word2 ` `=` `Words[i ` `+` `1` `] ` ` ` ` ` `# Find the first difference word1[k] != word2[k]. ` ` ` `for` `k ` `in` `range` `(` `min` `(` `len` `(word1), ` `len` `(word2))): ` ` ` ` ` `# If they compare false then it's not sorted. ` ` ` `if` `word1[k] !` `=` `word2[k]: ` ` ` `if` `Order_index[word1[k]] > Order_index[word2[k]]: ` ` ` `return` `False` ` ` `break` ` ` `else` `: ` ` ` ` ` `# If we didn't find a first difference, the ` ` ` `# Words are like ("add", "addition"). ` ` ` `if` `len` `(word1) > ` `len` `(word2): ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` ` ` `# Program Code ` `Words ` `=` `[` `"hello"` `, ` `"leetcode"` `] ` `Order ` `=` `"habcldefgijkmnopqrstuvwxyz"` ` ` `# Function call to print required answer ` `print` `(isAlienSorted(Words, Order)) ` |

*chevron_right*

*filter_none*

**Output:**

True

**Time Complexity:** O(N), where N is the total number of characters in all words.

**Auxiliary Space:** O(1)

## Recommended Posts:

- Find alphabetical order such that words can be considered sorted
- Check if the given string of words can be formed from words present in the dictionary
- Merge K sorted Doubly Linked List in Sorted Order
- Print words of a string in reverse order
- Sort the words in lexicographical order in Python
- Reorder the position of the words in alphabetical order
- Python | Sort words of sentence in ascending order
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values
- Merging two unsorted arrays in sorted order
- Print all permutations in sorted (lexicographic) order
- Rearrange a string in sorted order followed by the integer sum
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Print Binary Tree levels in sorted order
- Print array of strings in sorted order without copying one string into another

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.