-
Notifications
You must be signed in to change notification settings - Fork 0
/
3265_count_almost_equal_pairs.swift
45 lines (42 loc) · 1.13 KB
/
3265_count_almost_equal_pairs.swift
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
45
class Solution {
func countPairs(_ nums: [Int]) -> Int {
var count = 0
for i in 0 ..< nums.count {
for j in i+1 ..< nums.count {
if nums[i] == nums[j] || isAlmostEqual(nums[i], nums[j]) {
count += 1
}
}
}
return count
}
func isAlmostEqual(_ x: Int, _ y: Int) -> Bool {
// s1 variations
var a1 = Array(String(x))
for i in 0 ..< a1.count {
for j in i+1 ..< a1.count {
var a = a1
let temp = a[i]
a[i] = a[j]
a[j] = temp
if (Int(String(a)) == y) {
return true
}
}
}
// s2 variations
var a2 = Array(String(y))
for i in 0 ..< a2.count {
for j in i+1 ..< a2.count {
var a = a2
let temp = a[i]
a[i] = a[j]
a[j] = temp
if (Int(String(a)) == x) {
return true
}
}
}
return false
}
}