pyassistant

Group anagrams preserving order

Given a list of strings, group the anagrams together while preserving the original order of first appearance of each anagram group and the relative order of words within each group. Anagrams are words that contain the same characters with the same multiplicities (use case-sensitive comparison). Return a list of groups, where each group is a list of words that are anagrams of each other. Example: if a word appears later that belongs to an earlier group, it should be appended to that group's list rather than starting a new group.

Example:

Input:
['eat', 'tea', 'tan', 'ate', 'nat', 'bat']
Output:
[['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]

Make sure you return your solution, don't print!

AI

Bot

Trying to solve my challenge? Ask if you must, or press the purple button so I can analyze your code.