Dictionary | Sorting | Python easy solution with explanation
Intuition\nFind a way to associate similar words together. \nWe can utilize the word count approach but I preferred the approach where you sort the word. This a
# Intuition\n**Find a way to associate similar words together.** \nWe can utilize the **wo**rd **co**unt **ap**proach but I preferred the approach where you **so**rt the **wo**rd. This allows **an**agrams to be **so**rted and we can then **ma**tch the **wo**rds. \nFor example, \n\n`\'tan\' and \'nat\' when sorted would become \'ant\' and \'ant\'`\n\n\n\n# Approach\n1. Iterate over the \'strs\' and sort each word.\n2. Check if the sorted word exists as key in the dictionary/hashmap\n3. Because we are going to use the original word in the returned output,create a temp variable to store the sorted word.\n4. Now check if the temporary sorted word exists in the dictionary/hashmap\n5. If yes, append the word to the values list.\n6. If no, add a new key-value pair in the dictionary/hashmap.\n\n\n# Code\n```\nclass Solution:\n    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n        # Create a dictionary to act as hashmap\n        res = {}\n        for word in strs:\n            # We want to retain the original word to add to the dictionary\n            # Therefore, create a temporary variable with the sorted word\n            temp = \'\'.join(sorted(word))\n            # If the sorted word exists in the dictionary, \n            # append to the values list\n            if temp in res:\n                res[temp].append(word)\n            # Else, add a new key-value pair to the dictionary\n            else:\n                res[temp] = [word]\n        # We only require the values list to be returned\n        return res.values()\n\n```\n<br><br>\nPlease **up**vote if you find the approach and **ex**planation **us**eful!\n<br>
['Array', 'Hash Table', 'String', 'Sorting', 'Python3']