![]() |
VOOZH | about |
Given a list of words with lower cases. Implement a function to find all Words that have the same unique character set.
Example:
Input: words[] = { "may", "student", "students", "dog",
"studentssess", "god", "cat", "act",
"tab", "bat", "flow", "wolf", "lambs",
"amy", "yam", "balms", "looped",
"poodle"};
Output :
looped, poodle,
lambs, balms,
flow, wolf,
tab, bat,
may, amy, yam,
student, students, studentssess,
dog, god,
cat, act,
Explanation :
All words with same set of characters are printed together in a line.
The idea is to use hashing. We generate a key for all words. The key contains all unique characters (The size of the key is at most 26 for lowercase alphabets). We store indexes of words as values for a key. Once we have filled all keys and values in the hash table, we can print the result by traversing the table.
Below is the implementation of the above idea.
looped, poodle, student, students, studentssess, may, amy, yam, dog, god, cat, act, tab, bat, lambs, balms, flow, wolf,
Time Complexity:O(n*k) where n is number of words in dictionary and k is maximum length of a word.
Auxiliary Space:O(n*k), where n is number of words in dictionary and k is maximum length of a word.