
1. Both the strings contain only lowercase alphabets and can contain duplicates.
2. Return the uncommon characters in lexicographically sorted order.
The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.
The only line of each test case contains two strings, S1 and S2, separated by a single space.
For each test case, the uncommon characters are printed in sorted order.
The output for each test case is in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= |S1|, |S2| <= 50000
Where |S1| and |S2| are the lengths of the strings S1 and S2 respectively.
Time Limit: 1 sec
The basic solution for this problem involves using loops. We use a set, uncommonChars, to store the uncommon characters of the strings as this would reduce our effort on handling duplicates of a single uncommon alphabet (because a set does not contain duplicates). We solve the problem in two parts :
After the above 2 steps, the set uncommonChars will contain all the uncommon characters for both the strings.
The main idea behind this approach is to use hashing. We create a hash table of size 26 for all the lowercase alphabets and initialize all the elements as 0. Here, 0 denotes that the character is not present is either of the strings.
Now, we start traversing the first string and for every character, we update its value in the hash table as 1. Here, 1 indicates that the character is present in the first string.
After the above step, we start traversing the second string. For each character in the second string, we do the following:
Now, the characters that have a value as 1 or 2 in the hash table are the uncommon characters for the given two strings.