Input: nums =[13,23,12]Output: 4Explanation:
We have the following:\- The digit difference between **1**3 and **2**3is1.\- The digit difference between 1**3** and 1**2**is1.\- The digit difference between **23** and **12**is2.So the total sum of digit differences between all pairs of integers is`1 + 1
+ 2 = 4`.
Input: nums =[10,10,10,10]Output: 0Explanation:
All the integers in the array are the same. So the total sum of digit
differences between all pairs of integers will be 0.
For each digit position, count how many numbers have each digit (0-9). For every pair of numbers, if their digits at a position differ, it contributes 1 to the answer. For each position, the number of differing pairs is the number of pairs with different digits, which can be computed efficiently using counts.
classSolution {
public:longlong sumDigitDifferences(vector<int>& nums) {
int n = nums.size();
vector<string> s(n);
int m =0;
for (int i =0; i < n; ++i) {
s[i] = to_string(nums[i]);
m = max(m, (int)s[i].size());
}
for (int i =0; i < n; ++i) {
while (s[i].size() < m) s[i] ='0'+ s[i];
}
longlong ans =0;
for (int pos =0; pos < m; ++pos) {
vector<int> cnt(10);
for (int i =0; i < n; ++i) cnt[s[i][pos] -'0']++;
for (int d =0; d <10; ++d) {
ans +=1LL* cnt[d] * (n - cnt[d]);
}
}
return ans /2;
}
};
classSolution {
publiclongsumDigitDifferences(int[] nums) {
int n = nums.length;
String[] s =new String[n];
int m = 0;
for (int i = 0; i < n; i++) {
s[i]= String.valueOf(nums[i]);
m = Math.max(m, s[i].length());
}
for (int i = 0; i < n; i++) {
while (s[i].length() < m) s[i]="0"+ s[i];
}
long ans = 0;
for (int pos = 0; pos < m; pos++) {
int[] cnt =newint[10];
for (int i = 0; i < n; i++) cnt[s[i].charAt(pos) -'0']++;
for (int d = 0; d < 10; d++) ans += 1L * cnt[d]* (n - cnt[d]);
}
return ans / 2;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funsumDigitDifferences(nums: IntArray): Long {
val n = nums.size
val s = nums.map { it.toString() }.toMutableList()
var m = s.maxOf { it.length }
for (i in0 until n) {
while (s[i].length < m) s[i] = "0" + s[i]
}
var ans = 0Lfor (pos in0 until m) {
val cnt = IntArray(10)
for (i in0 until n) cnt[s[i][pos] - '0']++for (d in0..9) ans += cnt[d].toLong() * (n - cnt[d])
}
return ans / 2 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defsumDigitDifferences(self, nums: list[int]) -> int:
n = len(nums)
s = [str(x) for x in nums]
m = max(len(x) for x in s)
s = [x.zfill(m) for x in s]
ans =0for pos in range(m):
cnt = [0] *10for i in range(n):
cnt[int(s[i][pos])] +=1for d in range(10):
ans += cnt[d] * (n - cnt[d])
return ans //2
impl Solution {
pubfnsum_digit_differences(nums: Vec<i32>) -> i64 {
let n = nums.len();
letmut s: Vec<String>= nums.iter().map(|x| x.to_string()).collect();
let m = s.iter().map(|x| x.len()).max().unwrap();
for x in&mut s {
while x.len() < m {
*x =format!("0{}", x);
}
}
letmut ans =0i64;
for pos in0..m {
letmut cnt = [0; 10];
for i in0..n {
cnt[s[i].as_bytes()[pos] asusize-b'0'asusize] +=1;
}
for d in0..10 {
ans += cnt[d] asi64* (n asi64- cnt[d] asi64);
}
}
ans /2 }
}