Count how many unordered pairs of words differ in at most one position.
You are given several words. Treat two words as a matching pair if they have the same length and differ in at most one character position.
Count how many unordered pairs of distinct words form a matching pair.
A pair of words is unordered, so (i, j) is the same as (j, i).
Return the number of matching pairs among all distinct word pairs.
Example 1
Input
5 aaa aab abb abc aac
Output
4
Explanation
Matching pairs are: (aaa, aab), (aaa, aac), (aab, abb), and (abb, abc).
Example 2
Input
4 code code coda cope
Output
3
Explanation
Pairs are (code, code), (code, coda), and (code, cope).
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.