#3805
Count Caesar Cipher Pairs
medium · 50.9% accepted · 105 likes · top 39%
array · hash table · math · string · counting
Description
You are given an array words of n strings. Each string has length m and contains only lowercase English letters.
Two strings s and t are similar if we can apply the following operation any number of times (possibly zero times) so that s and t become equal.
- Choose either s or t.
- Replace every letter in the chosen string with the next letter in the alphabet cyclically. The next letter after 'z' is 'a'.
Count the number of pairs of indices (i, j) such that:
- i < j
- words[i] and words[j] are similar.
Return an integer denoting the number of such pairs.
Solution