Count how many pairs of strings match under a Caesar-cipher-style shift.
You are given an array of strings. Two strings are considered a matching pair if one can be turned into the other by shifting every letter by the same amount in the alphabet, with wraparound from z to a.
For example, shifting by 1 turns abc into bcd, and shifting by 2 turns xyz into zab.
Your task is to count the number of index pairs (i, j) with i < j such that words[i] and words[j] are matching pairs.
Two strings are only comparable if they have the same length.
words.1 <= words.lengthExample 1
Input
words = ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"]
Output
4
Explanation
The matching pairs are:
abc with bcdabc with xyzbcd with xyzaz with ba
Single-letter strings a and z also match, giving one more pair. Total: 4.Example 2
Input
words = ["aa", "bb", "cc"]
Output
3
Explanation
All three strings are equivalent under a uniform shift, so every pair matches: (aa, bb), (aa, cc), and (bb, cc).
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.