1223. Matching Letter Profiles
A word game groups words by how their letters repeat, ignoring which letters they are. The profile of a word is the ascending list of its non-zero letter counts. For example "aab" and "xyy" both have profile [1, 2], while "abc" has profile [1, 1, 1].
Given the array words, return the number of index pairs i < j whose words have equal profiles. Identical words count as equal profiles, and every word has a profile of its own, so words of different lengths never match. Return the count as a 64-bit integer.
Target roughly O(total characters) time using a hash map keyed by the profile.
Example 1
- Input:
- words = ["parrot","tomato","banana","kayak","rotor","level","zz"]
- Output:
- 3
- Explanation:
Profiles: kayak, rotor and level all have [1, 2, 2], giving 3 pairs, while parrot, tomato, banana and zz each have a profile no other word shares.
Example 2
- Input:
- words = ["qwe","lmn","tyu","kk"]
- Output:
- 3
- Explanation:
qwe, lmn and tyu all have profile [1, 1, 1] (3 pairs), and kk has profile [2] with no partner.
Example 3
- Input:
- words = ["zebra","apple","lolly","mimic","pepper","sees","noon"]
- Output:
- 1
- Explanation:
Only sees and noon match: both have profile [2, 2], and every other word has a different profile.
Constraints
1 ≤ words.length ≤ 500
1 ≤ words[i].length ≤ 20
words[i] consists of lowercase English letters.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(L + n log 26)
- Space
- O(n)