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.
アナグラムをグループ化
2つの単語がアナグラムとは、同じ文字をちょうど使っている場合です。例: 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. · 実行ボタンをクリックして出力を確認してください。