Group anagrams · 变位词分组
Group anagrams
Two words are anagrams if they use exactly the same letters, like eat and tea. Group the anagrams together and return a list of groups.
The trick: sort the letters of each word to make a key — eat and tea both become aet. Words with the same key belong in the same group. Use a dictionary from key → list of words, and remember the order the keys first appear.
变位词分组
如果两个单词使用的字母完全相同,它们就是变位词,比如 eat 和 tea。把变位词分到一组,并返回分组列表。
技巧:把每个单词的字母排序,作为键 —— eat 和 tea 都变成 aet。键相同的单词属于同一组。用一个从「键 → 单词列表」的字典,并记住键首次出现的顺序。
Write group_anagrams(words) that groups words which are anagrams of each other. Return a list of groups: words keep their original order inside a group, and groups are ordered by first appearance. · 编写 group_anagrams(words),把互为变位词(字母完全相同)的单词分到一组。返回分组列表:组内单词保持原始顺序,各组按首次出现的顺序排列。
Click Run to see the output here. · 点击“运行”查看此处输出。