COGNIZANT Coding Question β Solved
On a popular discussion platform, some users with harmful intentions use anagram-based methods to hide certain banned terms. An anagram is a word or phrase created by rearranging the letters of another word or phrase, using all the original letters exactly once. Implement a function to identify and group anagrams within the network's data log to detect these concealment attempts.
Write a Python function named groupAnagrams that takes a list of words (network log entries) as input and groups them into sets of anagrams.
Example: word_list = ['debitcard', 'badcred[s', 'cat', 'tac', 'act']
After grouping the anagrams, return [['debitcard', 'badcredit'], ['cat', 'tac', 'act']]. The provided code sorts the results and prints [['act', 'cat', 'tac'], ['badcredit', 'debitcard']].
Function Description: Complete the function group_anagrams in the editor with the following parameter(s): string words_list[n]: a list of words to group
Constraints: Each word_list[i] consists only of characters in the range ascii[a-z].
Input Format For Custom Testing:
Sample Case 0
Sample Input For Custom Testing:
2
act kitchen ran cat
listen silent enlist heart earth hater
good dog
Sample Output:
[['act', 'cat'], ['kitchen'], ['ran']]
[['dog'], ['good'], ['earth', 'hater', 'heart'], ['enlist', 'listen', 'silent']]
Explanation: Words that are anagrams are grouped into lists, e.g., 'act' and 'cat' in the first list of words. Note that 'dog' and 'good' do not have anagrams.
Sample Case 1
Sample Input For Custom Testing
Asked in:
COGNIZANT