3805. Count Caesar Cipher Pairs Medium

@problem@discussion
#Array#Hash Table#Math#String#Counting



1/**
2 * [3805] Count Caesar Cipher Pairs
3 *
4 * You are given an array words of n strings. Each string has length m and contains only lowercase English letters.
5 * 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.
6 * 
7 * 	Choose either s or t.
8 * 	Replace every letter in the chosen string with the next letter in the alphabet cyclically. The next letter after 'z' is 'a'.
9 * 
10 * Count the number of pairs of indices (i, j) such that:
11 * 
12 * 	i < j
13 * 	words[i] and words[j] are similar.
14 * 
15 * Return an integer denoting the number of such pairs.
16 *  
17 * <strong class="example">Example 1:
18 * <div class="example-block">
19 * Input: <span class="example-io">words = ["fusion","layout"]</span>
20 * Output: <span class="example-io">1</span>
21 * Explanation:
22 * words[0] = "fusion" and words[1] = "layout" are similar because we can apply the operation to "fusion" 6 times. The string "fusion" changes as follows.
23 * 
24 * 	"fusion"
25 * 	"gvtjpo"
26 * 	"hwukqp"
27 * 	"ixvlrq"
28 * 	"jywmsr"
29 * 	"kzxnts"
30 * 	"layout"
31 * </div>
32 * <strong class="example">Example 2:
33 * <div class="example-block">
34 * Input: <span class="example-io">words = ["ab","aa","za","aa"]</span>
35 * Output: <span class="example-io">2</span>
36 * Explanation:
37 * words[0] = "ab" and words[2] = "za" are similar. words[1] = "aa" and words[3] = "aa" are similar.
38 * </div>
39 *  
40 * Constraints:
41 * 
42 * 	1 <= n == words.length <= 10^5
43 * 	1 <= m == words[i].length <= 10^5
44 * 	1 <= n * m <= 10^5
45 * 	words[i] consists only of lowercase English letters.
46 * 
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/count-caesar-cipher-pairs/
51// discuss: https://leetcode.com/problems/count-caesar-cipher-pairs/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56    pub fn count_pairs(words: Vec<String>) -> i64 {
57        
58    }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_3805() {
69    }
70}
71

Back
© 2026 bowen.ge All Rights Reserved.