


If ‘WORDS’ = ["word","world","row"], ‘ORDER’ = "worldabcefghijkmnpqstuvxyz",the answer will be ‘NO’ as first and second words are not lexicographically sorted as ‘l’ comes before ‘d’ in alien language.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer, ‘N’ denoting the number of words.
The second line of each test case contains ‘N’ strings corresponding to ‘WORDS’.
The third line contains a permutation of 26 letters denoting the ‘ORDER’ string.
For each test case, print ‘YES’ if the words are sorted, else print ‘NO’.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 1000.
1 <= length of ‘WORDS[i]’ <= 20
Time limit: 1 sec
To check the lexicographical order, we will check that if the adjacent words are in the correct order or not. We will first store the rank of each letter of alien language in an array ‘RANK’.
If the rank of ‘A’ is greater than the rank of ‘B’ it implies A comes after B in the alien language.
We will iterate through the ‘ORDER’ string and set the RANK[ORDER[i]] as i.
Now, we will compare the two adjacent words and if we found any mismatch in the order, return ‘FALSE’.
Else, return ‘TRUE’
In this approach,we will first transform each word of ‘WORDS’ according to the rank of the letter in Alien language.
For example, if the ‘ORDER’ string is “worldabcefghijkmnpqstuvxyz”,so all the ‘w’ in ‘WORDS’ will be transformed into ‘a’ , all the ‘o’ will be transformed into ‘b’, and so on.
After transformation, we can check the lexicographical order of adjacent words using simple <,> operators.