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.
กลุ่ม anagram
สองคำเป็น anagram หากใช้ตัวอักษรเหมือนกันเป๊ะ, เช่น eat และ tea. Group anagram เข้าด้วยกันและ return ลิสต์ของกลุ่ม
เทคนิค: sort ตัวอักษรของแต่ละคำเพื่อทำเป็น key — eat และ tea กลายเป็น aet. คำที่มี key เดียวกันอยู่ในกลุ่มเดียวกัน. ใช้ dict จาก key → list of คำ, และจำลำดับที่ key ปรากฏครั้งแรก
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)为了จัดกลุ่มคำที่เป็น anagram กัน กลับรายการของกลุ่ม: คำคงลำดับเดิมภายในกลุ่ม และกลุ่มเรียงตามการปรากฏครั้งแรก
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่