Alien Dictionary Problem Statement
Ninja is mastering an unusual language called the Alien Language. Although it uses the same alphabets as English, the sequence of these alphabets is different. This sequence is defined by the string ORDER
. Ninja needs to verify if the words in the array WORDS
are sorted lexicographically according to this alien language.
Input:
- An integer 'T', representing the number of test cases.
- For each test case, the first line contains an integer 'N', indicating the number of words.
- The second line has 'N' space-separated strings representing 'WORDS'.
- The third line contains the 'ORDER' string, which is a permutation of 26 letters.
Output:
For each test case, print 'YES' if the words are sorted according to the alien language order, otherwise print 'NO'.
Example:
Input:
WORDS = ["word","world","row"], ORDER = "worldabcefghijkmnpqstuvxyz"
Output:
NO (as 'l' comes before 'd' in the given order)
Constraints:
1 <= T <= 10
1 <= N <= 1000
1 <= length of WORDS[i] <= 20
- Time limit: 1 sec
Note:
No need to handle the input/output as it's already managed. Focus on implementing the function to solve the problem.

AnswerBot
1y
The task is to check whether the given words are sorted lexicographically in an alien language.
Read the number of test cases
For each test case, read the number of words, the words themselves, and the ...read more
Prince Kumar
3mo
let arr=["word","world","row"]; let order='worldabcefghijkmnpqstuvxyz' let orderMap=new Map(); for(var i=0;i<order.length;i++){ orderMap.set(order[i],i) } function compare(word1,word2){ const minLengt...read more
Help your peers!
Add answer anonymously...
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

