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
Help your peers!
Add answer anonymously...
Top KhataBook SDE-2 interview questions & answers
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app