All DSA problems
MediumHashingAmazonUberBloomberg

Group Anagrams Logic

Given n words, group them into anagram groups. Print the number of distinct anagram groups.

Two words are anagrams if they contain the same characters with same frequencies (order ignored).

**Input**

First line: n

Next n lines: one word each

Examples

Example 1

Input:

6
eat
tea
tan
ate
nat
bat

Output: 3

Groups: [eat,tea,ate], [tan,nat], [bat]

Constraints

  • 1 ≤ n ≤ 1000
  • 1 ≤ word length ≤ 100
  • lowercase letters only

Target: O(n · k log k) time · O(n · k) space