-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3267-count-almost-equal-pairs-ii.cpp
More file actions
33 lines (32 loc) · 1.1 KB
/
Copy path3267-count-almost-equal-pairs-ii.cpp
File metadata and controls
33 lines (32 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
int countPairs(vector<int>& nums) {
int ans = 0, n = (int) nums.size();
sort(nums.begin(), nums.end());
vector<unordered_set<int>> vars(n);
unordered_map<int, int> mp;
for (int i = 0; i < n; i++) {
string s = to_string(nums[i]);
int sz = (int) s.size();
for (int x = 0; x < sz; x++) {
for (int y = x; y < sz; y++) {
for (int j = 0; j < sz; j++) {
for (int k = j; k < sz; k++) {
swap(s[x], s[y]);
swap(s[j], s[k]);
int cand = stoi(s);
if (vars[i].find(cand) == vars[i].end()) {
ans += mp[cand];
vars[i].insert(cand);
}
swap(s[j], s[k]);
swap(s[x], s[y]);
}
}
}
}
mp[nums[i]]++;
}
return ans;
}
};