Case-Folded Anagram Grouping in Deterministic Order

Difficulty: Easy · Unit 1: Core Foundations & Text · Featured in: CodeTantra Lab Pattern

Given N words, group words that are anagrams of each other (ignoring letter case). Within each group, preserve the original input words sorted lexicographically (case-sensitive tie-break after lowercase). Print each group on its own line, with groups ordered by their smallest member.

Input & Output Format

Input: Line 1: integer N. Line 2: N space-separated words.

Output: One line per anagram group, words separated by single spaces.

Sample 1

6
eat Tea tan ate nat bat
Tea ate eat
bat
nat tan

Sorted inside each group: ['Tea', 'ate', 'eat'], ['bat'], ['nat', 'tan'], ordered by first word.

Free forever — sign in with email at poorvithmp.com to unlock the dry run, verified Python 3 solution, edge-case traps, and downloads.

Back to Python Notes Hub