3267. Count Almost Equal Pairs II Hard

@problem@discussion
#Array#Hash Table#Sorting#Counting#Enumeration



1/**
2 * [3267] Count Almost Equal Pairs II
3 *
4 * Attention: In this version, the number of operations that can be performed, has been increased to twice.<!-- notionvc: 278e7cb2-3b05-42fa-8ae9-65f5fd6f7585 -->
5 * You are given an array nums consisting of positive integers.
6 * We call two integers x and y almost equal if both integers can become equal after performing the following operation at most <u>twice</u>:
7 * 
8 * 	Choose either x or y and swap any two digits within the chosen number.
9 * 
10 * Return the number of indices i and j in nums where i < j such that nums[i] and nums[j] are almost equal.
11 * Note that it is allowed for an integer to have leading zeros after performing an operation.
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">nums = [1023,2310,2130,213]</span>
16 * Output: <span class="example-io">4</span>
17 * Explanation:
18 * The almost equal pairs of elements are:
19 * 
20 * 	1023 and 2310. By swapping the digits 1 and 2, and then the digits 0 and 3 in 1023, you get 2310.
21 * 	1023 and 213. By swapping the digits 1 and 0, and then the digits 1 and 2 in 1023, you get 0213, which is 213.
22 * 	2310 and 213. By swapping the digits 2 and 0, and then the digits 3 and 2 in 2310, you get 0213, which is 213.
23 * 	2310 and 2130. By swapping the digits 3 and 1 in 2310, you get 2130.
24 * </div>
25 * <strong class="example">Example 2:
26 * <div class="example-block">
27 * Input: <span class="example-io">nums = [1,10,100]</span>
28 * Output: <span class="example-io">3</span>
29 * Explanation:
30 * The almost equal pairs of elements are:
31 * 
32 * 	1 and 10. By swapping the digits 1 and 0 in 10, you get 01 which is 1.
33 * 	1 and 100. By swapping the second 0 with the digit 1 in 100, you get 001, which is 1.
34 * 	10 and 100. By swapping the first 0 with the digit 1 in 100, you get 010, which is 10.
35 * </div>
36 *  
37 * Constraints:
38 * 
39 * 	2 <= nums.length <= 5000
40 * 	1 <= nums[i] < 10^7
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/count-almost-equal-pairs-ii/
46// discuss: https://leetcode.com/problems/count-almost-equal-pairs-ii/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn count_pairs(nums: Vec<i32>) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_3267() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.