Challenge: Sort the Elements of an Array using a Trie

If you are given an array of strings, can you sort its elements using a Trie data structure? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first.

Problem Statement

In this problem, you have to implement the sortArray() function to sort the elements of an array of strings in alphabetical order.

Function Prototype

ArrayList<String> sortArray(String []arr);

Here, arr is a String array

Output

It returns the given array, sorted and in an ArrayList form.

Sample Input

String keys[] = {"the", "a", "there", "answer", "any",
                     "by", "bye", "their","abc"};

Sample Output

{"a", "abc", "answer", "any", "by", "bye", "the", "their", "there"}

Explanation

There are 9 words total in the given keys array, so we need to sort them alphabetically before returning the list with strings in sorted order.

Here’s an illustration of the given challenge:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.