Group anagrams · تجميع الأنagrams
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) تُجمع كلمات هي أنagrams لبعضها البعض. ارجع قائمة مجموعات: الكلمات تحافظ على ترتيبها الأصلي داخل المجموعة، والمجموعات مرتبة حسب أول ظهور.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.