Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given a sorted dictionary of an alien language having N words and k starting alphabets of standard dictionary. Find the order of characters in the alien language.

**Note:** Many orders may be possible for a particular test case, thus you may return any valid order and output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned.

**Example 1:**

Input:N = 5, K = 4 dict = {"baa","abcd","abca","cab","cad"}Output:1Explanation:Here order of characters is 'b', 'd', 'a', 'c' Note that words are sorted and in the given language "baa" comes before "abcd", therefore 'b' is before 'a' in output. Similarly we can find other orders.

**Example 2:**

**Input:
**N = 3, K = 3
dict = {"caa","aaa","aab"}
**Output:
**1
**Explanation:
**Here order of characters is
'c', 'a', 'b' Note that words are sorted
and in the given language "caa" comes before
"aaa", therefore 'c' is before 'a' in output.
Similarly we can find other orders.

**Your Task:**

You don't need to read or print anything. Your task is to complete the function **findOrder() **which takes the string array dict[], its size N and the integer K as input parameter and returns a string denoting the order of characters in the alien language.

**Expected Time Complexity: **O(N * |S| + K) , where |S| denotes maximum length.

**Expected Space Compelxity: **O(K)

**Constraints:**

1 ≤ N, M ≤ 300

1 ≤ K ≤ 26

1 ≤ Length of words ≤ 50

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Alien Dictionary

...