-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathSolution.swift
47 lines (41 loc) · 1.01 KB
/
Solution.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
46
47
class Solution {
private var parent: [Int] = []
func numSimilarGroups(_ strs: [String]) -> Int {
let n = strs.count
parent = Array(0..<n)
for i in 0..<n {
for j in (i + 1)..<n {
if check(strs[i], strs[j]) {
parent[find(i)] = find(j)
}
}
}
var groups = 0
for i in 0..<n {
if i == find(i) {
groups += 1
}
}
return groups
}
private func check(_ a: String, _ b: String) -> Bool {
let n = a.count
var count = 0
let arrA = Array(a), arrB = Array(b)
for i in 0..<n {
if arrA[i] != arrB[i] {
count += 1
}
if count > 2 {
return false
}
}
return count <= 2
}
private func find(_ x: Int) -> Int {
if parent[x] != x {
parent[x] = find(parent[x])
}
return parent[x]
}
}