COGNIZANT Coding Question – Solved

5 Live
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

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


def group_anagrams(words_list):
    # Write your code here
    h = {}
    for word in words_list:
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach the problem of grouping anagrams from a list of words, first understand what anagrams are and how they relate to the problem statement. Anagrams are words or phrases formed by rearranging the letters of another word or phrase using all the original letters exactly once. In this context, the goal is to group all words that are anagrams of each other together.

The key insight to solving this problem is recognizing that all anagrams share a common property: when their letters are sorted in a consistent order (e.g., alphabetical), they become identical strings. For example, the words "act", "cat", and "tac" all become "act" when their letters are sorted. This property can serve as the foundation for grouping words efficiently.

Begin by thinking about how to represent each word in a way that allows you to identify anagrams quickly. Since sorting the letters of a word produces a canonical form that is identical for all its anagrams, this sorted string can be used as a key or identifier for grouping.

The general approach can be broken down into the following steps:

1. **Iterate over each word in the input list:**
For every word, process it independently to find its canonical representation.

2. **Convert the word into its canonical form:**
Sort the characters of the word alphabetically to create a standardized key. For example, "debitcard" becomes "abcddeirt", and "badcredit" (assuming the example has a typo and meant to be "badcredit") becomes the same sorted string. This sorted form will be the identifier under which all anagrams of that word will be grouped.

3. **Group words by their canonical form:**
Use a data structure such as a dictionary or hash map where keys are the sorted strings and values are lists of words that correspond to those keys. For each word, add it to the list corresponding to its sorted key.

4. **Collect the grouped anagrams:**
After processing all words, the values in this dictionary represent groups of words that are anagrams of each other. Extract these lists to form the final output.

5. **Consider the output format and ordering:**
Depending on the problem requirements, you may need to sort each group internally or sort the entire collection of groups. The example output shows sorted groups and groups sorted by their first element for readability and consistency.

As you think through this problem, consider some important details and edge cases:

- **Words without any anagram pairs:**
Words that do not share their sorted key with any other word will be in groups by themselves. This means singletons are also valid groups.

- **Handling duplicates:**
If the input contains duplicate words, decide whether to keep them multiple times or remove duplicates. Usually, keeping duplicates maintains data fidelity.

- **Efficiency considerations:**
Sorting each word has a time complexity proportional to the length of the word times log of the word length. Since each word is processed once, the overall complexity will depend on the number of words and the average word length. This is efficient for typical constraints.

- **Using alternative keys:**
Instead of sorting, another approach might be counting character frequencies and using a frequency tuple as the key, which can be more efficient for longer words. However, sorting is simpler and intuitive.

- **Data structures:**
Use a hash map or dictionary where keys are the sorted forms and values are lists of words. This enables O(1) average time complexity for insertion and lookup.

- **Output consistency:**
Make sure to maintain the order of words within each group if required or sort the groups for consistent output, especially if you want reproducible results for testing.

In summary, the conceptual roadmap is:

- Transform each word into a canonical sorted form.
- Use this canonical form as a grouping key in a dictionary.
- Append words to their corresponding groups based on the key.
- Extract all groups as the final result.

This approach leverages the fundamental property of anagrams and efficient dictionary lookups, enabling grouping of words that are anagrams in linear time relative to the number of words, with a manageable overhead due to sorting each word’s letters.
```


Related Questions