You are given an array of strings 'ARRSTR[]' of size 'N' and a character 'C'. Your task is to sort the 'ARRSTR[]' according to the new alphabetical order that starts with the given character 'C'.
Note:
1) The character ‘C’ is a lowercase English alphabet that is given as input.
2) For example, if the character is 'C' is "d" then, the alphabetical order starts with "d" will look like {d,e,f,....,y,z,a,b,c}.
3) Every string in the array consists of only lowercase English alphabets.
The first line contains an integer 'T', which denotes the number of test cases or queries to be run. Then, the 'T' test cases follow.
The first line of each test case contains a positive integer 'N' denoting the size of the array and a character 'C', as described in the problem statement.
The second line of each test case contains a sequence of 'N' space-separated strings denoting the elements of the array.
Output Format:
For each test case, print in a new line a sequence of 'N' space-separated strings after sorting according to the new alphabetical order given in the problem.
Output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 1000
'C' is a lowercase English alphabet.
1 <= | ARRSTR[i] | <= 9, where 1 <= i <= N.
Time Limit: 1sec
1
4 h
please accept my offer
my offer please accept
In the first test case, the new alphabetical order that starts with "h" is “hijklmnopqrstuvwxyzabcdefg”. So, the order of the characters ‘p’, ‘a’, ‘m’, ‘o’ in the new alphabetical order is ‘m’ → ‘o’ → ‘p’ → ‘a’. Hence, the answer is “my offer please accept”.
2
2 c
ninjas coding
1 z
world
coding ninjas
world
In the first test case, according to the new alphabetical order, ‘c’ comes first, then ‘n’, so coding comes ahead of ninjas.
In the second test case, according to the new alphabetical order, ‘z’ comes first. As there is only one word so there would be no change in sentence.
Can you think of using some data structure to store the order?
Approach: The idea here is to use a data structure HashMap to store each character’s order in the new alphabetical order, starting with a letter c (given in the problem). Now, start comparing the strings in the ‘ARRSTR[]’ using HashMap that has been created to define the order.
Suppose that we are comparing two strings of ‘ARRSTR[]’ named ‘X’ and ‘Y’. Let us assume that we have found the first different character at the ith index, so if the order (already stored in the HashMap) of ‘X[i]’ is less than the ‘Y[i]’, then ‘X’ is to placed in the front of ‘Y’ (not necessarily adjacent to ‘Y’), and vice-versa.
Algorithm is as follows:
bool compare(string x, string y) :
O(N*logN), where N is the size of the array.
We are sorting the array of strings of length N, which takes O(N log N) time. Since the length of each string <= 9, we neglect this while calculating the time complexity. Hence, the overall time complexity is O(N log N).
O(1).
We are using a string newOrder and a hashmap. Both are of size 26 only, which is independent of the size of the input. Hence, the space complexity is O(1).