Solving Leetcode Interviews in Seconds with AI: Count Almost Equal Pairs I
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3265" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
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 = [3,12,30,17,21] Output: 2 Explanation: The almost equal pairs of elements are: 3 and 30. By swapping 3 and 0 in 30, you get 3. 12 and 21. By swapping 1 and 2 in 12, you get 21. Example 2: Input: nums = [1,1,1,1,1] Output: 10 Explanation: Every two elements in the array are almost equal. Example 3: Input: nums = [123,231] Output: 0 Explanation: We cannot swap any two digits of 123 or 231 to reach the other. Constraints: 2 <= nums.length <= 100 1 <= nums[i] <= 106
Explanation
Here's the breakdown of the solution:
- String Conversion and Length Check: Convert the integers to strings for easy digit manipulation. Only compare numbers that have the same number of digits.
- Direct Equality and Single Swap Check: First, if numbers are already equal, increment the count. Otherwise, check if a single swap of digits within either number can make them equal.
Counting Pairs: Count the number of pairs (i, j) where i < j and the numbers at those indices are almost equal.
Runtime Complexity: O(N2 K), where N is the length of
numsand K is the maximum number of digits in a number. *Storage Complexity: O(1).
def areAlmostEqual(s1, s2):
if s1 == s2:
return True
diff_indices = []
for i in range(len(s1)):
if s1[i] != s2[i]:
diff_indices.append(i)
if len(diff_indices) == 2:
i, j = diff_indices
if s1[i] == s2[j] and s1[j] == s2[i]:
return True
return False
def solve():
nums = [int(x) for x in input().split(",")]
n = len(nums)
count = 0
for i in range(n):
for j in range(i + 1, n):
s1 = str(nums[i])
s2 = str(nums[j])
if len(s1) != len(s2):
continue
if areAlmostEqual(s1, s2):
count += 1
return count
def almost_equal_pairs(nums):
n = len(nums)
count = 0
for i in range(n):
for j in range(i + 1, n):
s1 = str(nums[i])
s2 = str(nums[j])
if len(s1) != len(s2):
continue
if s1 == s2:
count += 1
continue
def check_swap(str1, str2):
diff = []
for k in range(len(str1)):
if str1[k] != str2[k]:
diff.append(k)
if len(diff) == 2:
i1, i2 = diff
temp_list = list(str1)
temp_list[i1], temp_list[i2] = temp_list[i2], temp_list[i1]
swapped_str = "".join(temp_list)
return swapped_str == str2
return False
if check_swap(s1, s2) or check_swap(s2, s1):
count += 1
return count
</code>
# Code
```python
None