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. · לחץ על הרץ כדי לראות את התוצא כאן.