Count Almost Equal Pairs I - Complete Solution Guide
Count Almost Equal Pairs I is LeetCode problem 3265, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an array nums consisting of positive integers. We call two integers x and y in this problem almost equal if both integers can become equal after performing the following operation at most once : Choose either x or y and swap any two digits within the chosen number. Return the number of indices i and j in nums where i < j such that nums[i] and nums[j] are almost equal . Note that it is allowed for an integer to have leading zeros after performing an operation. Example 1: Input: nums
Detailed Explanation
The problem requires us to count the number of almost equal pairs in a given array of positive integers. Two integers are considered 'almost equal' if they are equal, or can become equal by swapping two digits within either of the numbers at most once. The comparison should only be done between pairs of numbers with indices i and j where i < j. The numbers can have leading zeros after the swap.
Solution Approach
The solution iterates through all possible pairs (i, j) in the input array `nums` where i < j. For each pair, it calls a helper function `are_almost_equal` to determine if the two numbers at those indices are almost equal. The `are_almost_equal` function converts the integers to strings, pads them with leading zeros if necessary to ensure equal length, and then compares them character by character. If the strings are identical, they are almost equal. If there are exactly two different characters, it checks if swapping them would make the strings identical. If so, the numbers are almost equal. Finally, the outer loop increments a counter whenever an almost equal pair is found, returning the final count.
Step-by-Step Algorithm
- Step 1: Iterate through the `nums` array with nested loops to consider all pairs (i, j) such that i < j.
- Step 2: For each pair (nums[i], nums[j]), call the `are_almost_equal` function.
- Step 3: Inside `are_almost_equal`, convert both integers to strings.
- Step 4: Determine the maximum length of the two strings.
- Step 5: Pad both strings with leading zeros until their lengths match the maximum length.
- Step 6: If the padded strings are identical, return `true`.
- Step 7: If the strings are not identical, identify the indices where the characters differ.
- Step 8: If the number of differing indices is exactly two, check if swapping the characters at those indices in one of the strings would make the strings identical.
- Step 9: If the swap would make the strings identical, return `true`.
- Step 10: If any of the conditions for almost equality are not met, return `false`.
- Step 11: In the outer loop, if `are_almost_equal` returns `true`, increment the counter.
- Step 12: After iterating through all pairs, return the final count.
Key Insights
- Insight 1: The core is to implement a function that checks if two numbers are 'almost equal' efficiently.
- Insight 2: Converting numbers to strings makes digit manipulation and comparison easier.
- Insight 3: Padding strings with leading zeros simplifies the comparison when the numbers have different lengths.
- Insight 4: The number of different digits should be checked whether it is equal to zero or two.
Complexity Analysis
Time Complexity: O(n^2 * log(max_num))
Space Complexity: O(log(max_num))
Topics
This problem involves: Array, Hash Table, Sorting, Counting, Enumeration.
Companies
Asked at: Info Edge.