Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@avaiesshaik782 | 786 |

@arpitsatnalika | 777 |

@tujhekya | 602 |

@prakharxyz | 601 |

@code_it_ | 591 |

@iamronak | 505 |

@imnm | 500 |

@pradeepkumarjad | 488 |

@satyam0460 | 481 |

@nitesha125 | 468 |

@Rahul Jadhav 1 | 466 |

Complete Leaderboard |

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.

**Input:**

The first line of input contains an integer **T** denoting the no of test cases. Then T test cases follow. Each test case contains an integer **N** and **k** denoting the size of the dictionary. Then in the next line are sorted space separated values of the alien dictionary.

**Output:**

For each test case in a new line output will be 1 if the order of string returned by the function is correct else 0 denoting incorrect string returned.

**Your Task:**

You don't need to read input 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 inputs and returns a string denoting the order of characters in the alien language.

**Expected Time Complexity:** O(N + K).

**Expected Auxiliary Space:** O(K).

**Constraints:**

1 <= T <= 1000

1 <= N <= 300

1 <= k <= 26

1 <= Length of words <= 50

**Example:
Input:**

2

5 4

baa abcd abca cab cad

3 3

caa aaa aab

**Output:**

1

1

**Explanation:
Test Case 1:**

Input: Dict[ ] = { "baa", "abcd", "abca", "cab", "cad" }, k = 4

Output: Function returns "bdac"

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.

**Test Case 2:**

Input: Dict[] = { "caa", "aaa", "aab" }, k = 3

Output: Function returns "cab"

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?

Yes
Alien Dictionary

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.