-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3265-count-almost-equal-pairs-i.cpp
More file actions
44 lines (36 loc) · 1.19 KB
/
3265-count-almost-equal-pairs-i.cpp
File metadata and controls
44 lines (36 loc) · 1.19 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
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
int countPairs(vector<int>& nums) {
int ans = 0, n = (int) nums.size();
auto check = [&](int i, int j) -> bool {
string s = to_string(nums[i]), t = to_string(nums[j]);
for (int x = 0; x < s.size(); x++) {
for (int y = x; y < s.size(); y++) {
string cs = s;
cs[x] = s[y];
cs[y] = s[x];
if (stoi(cs) == stoi(t)) {
return true;
}
}
}
for (int x = 0; x < t.size(); x++) {
for (int y = x; y < t.size(); y++) {
string ct = t;
ct[x] = t[y];
ct[y] = t[x];
if (stoi(ct) == stoi(s)) {
return true;
}
}
}
return false;
};
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ans += check(i, j);
}
}
return ans;
}
};