Medium
You are given an array nums
consisting of positive integers where all integers have the same number of digits.
The digit difference between two integers is the count of different digits that are in the same position in the two integers.
Return the sum of the digit differences between all pairs of integers in nums
.
Example 1:
Input: nums = [13,23,12]
Output: 4
Explanation: We have the following:
1 + 1 + 2 = 4
.Example 2:
Input: nums = [10,10,10,10]
Output: 0
Explanation: All the integers in the array are the same. So the total sum of digit differences between all pairs of integers will be 0.
Constraints:
2 <= nums.length <= 105
1 <= nums[i] < 109
nums
have the same number of digits.class Solution {
fun sumDigitDifferences(nums: IntArray): Long {
var result: Long = 0
while (nums[0] > 0) {
val counts = IntArray(10)
for (i in nums.indices) {
val digit = nums[i] % 10
nums[i] = nums[i] / 10
result += (i - counts[digit]).toLong()
counts[digit]++
}
}
return result
}
}