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”.
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 > words, 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 :
Time Complexity: O(N), where N is the total number of characters in all words.
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Reverse String according to the number of words
- Merge K sorted Doubly Linked List in Sorted Order
- Count words that appear exactly two times in an array of words
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Sort an array according to the order defined by another array
- Sort a string according to the order defined by another string
- Sort the array of strings according to alphabetical order defined by another string
- Print elements of an array according to the order defined by another array | set 2
- Remove duplicates from string keeping the order according to last occurrences
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values
- Check if count of Alphabets and count of Numbers are equal in the given String
- Check if a string contains only alphabets in Java
- Check if two sorted arrays can be merged to form a sorted array with no adjacent pair from the same array
- Check if value exists in level-order sorted complete binary tree
- Print words of a string in reverse order
- Sort the words in lexicographical order in Python
- Python | Sort words of sentence in ascending order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.