Count Caesar Cipher Pairs
You are given an array words of n strings, where each string has a length of m and contains only lowercase English letters.
Two strings s and t are considered similar if you can apply a Caesar cipher shift to one of them to make them exactly equal. An operation consists of replacing every single letter in the chosen string with the next letter in the alphabet (wrapping around from 'z' back to 'a').
Return the total number of index pairs (i, j) such that i < j and words[i] is similar to words[j].
Example 1:
Input: words = ["abc", "bcd", "cde"]
Output: 3
Explanation:
"abc" shifted by 1 is "bcd", and shifted by 2 is "cde". All 3 strings can be shifted to match one another, forming 3 valid pairs.
Example 2:
Input: words = ["hello", "world", "ifmmp"]
Output: 1
Explanation:
Shifting "hello" forward by exactly 1 position yields "ifmmp". "world" does not match either string under any shift. Total pairs = 1.
Now Your Turn!
Pick the correct output for the given inputInput: words = ["a", "b", "c"]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n == words.length <= 105
- 1 <= m == words[i].length <= 105
- 1 <= n * m <= 105
- words[i] consists only of lowercase English letters.