@@ -119,26 +119,17 @@ d[find(a)] = distance
119
119
``` python
120
120
class Solution :
121
121
def numSimilarGroups (self , strs : List[str ]) -> int :
122
- n = len (strs)
123
- p = list (range (n))
124
-
125
122
def find (x ):
126
123
if p[x] != x:
127
124
p[x] = find(p[x])
128
125
return p[x]
129
-
130
- def check (a , b ):
131
- cnt = 0
132
- for i, c in enumerate (a):
133
- if c != b[i]:
134
- cnt += 1
135
- return cnt <= 2
136
-
126
+
127
+ n, l = len (strs), len (strs[0 ])
128
+ p = list (range (n))
137
129
for i in range (n):
138
130
for j in range (i + 1 , n):
139
- if check (strs[i], strs[j]) :
131
+ if sum (strs[i][k] != strs[j][k] for k in range (l)) <= 2 :
140
132
p[find(i)] = find(j)
141
-
142
133
return sum (i == find(i) for i in range (n))
143
134
```
144
135
@@ -198,30 +189,27 @@ class Solution {
198
189
class Solution {
199
190
public:
200
191
vector<int > p;
192
+
201
193
int numSimilarGroups(vector<string>& strs) {
202
194
int n = strs.size();
203
- for (int i = 0; i < n; ++i) p.push_back(i);
195
+ p.resize(n);
196
+ for (int i = 0; i < n; ++i) p[i] = i;
204
197
for (int i = 0; i < n; ++i)
205
- {
206
198
for (int j = i + 1; j < n; ++j)
207
- {
208
199
if (check(strs[i], strs[j]))
209
200
p[find(i)] = find(j);
210
- }
211
- }
212
- int res = 0;
201
+ int ans = 0;
213
202
for (int i = 0; i < n; ++i)
214
- {
215
- if (i == find(i)) ++res;
216
- }
217
- return res;
203
+ if (i == find(i))
204
+ ++ans;
205
+ return ans;
218
206
}
219
207
220
208
bool check (string a, string b) {
221
209
int cnt = 0;
222
- int n = a.size();
223
- for (int i = 0; i < n; ++i )
224
- if (a[i] != b[i]) ++cnt;
210
+ for ( int i = 0; i < a.size(); ++i)
211
+ if (a [ i ] != b [ i ] )
212
+ ++cnt;
225
213
return cnt <= 2;
226
214
}
227
215
@@ -235,46 +223,42 @@ public:
235
223
### **Go**
236
224
237
225
```go
238
- var p []int
239
-
240
226
func numSimilarGroups(strs []string) int {
241
227
n := len(strs)
242
- p = make([]int, n)
243
- for i := 0; i < n; i++ {
228
+ p : = make([]int, n)
229
+ for i := range p {
244
230
p[i] = i
245
231
}
246
- for i := 0; i < n; i++ {
247
- for j := i + 1; j < n; j++ {
248
- if !check(strs[i], strs[j]) {
249
- continue
232
+ check := func(a, b string) bool {
233
+ cnt := 0
234
+ for i := range a {
235
+ if a[i] != b[i] {
236
+ cnt++
250
237
}
251
- p[find(i)] = find(j)
252
238
}
239
+ return cnt <= 2
253
240
}
254
- res := 0
255
- for i := 0; i < n; i++ {
256
- if i == find(i) {
257
- res++
241
+ var find func(x int) int
242
+ find = func(x int) int {
243
+ if p[x] != x {
244
+ p[x] = find(p[x])
258
245
}
246
+ return p[x]
259
247
}
260
- return res
261
- }
262
-
263
- func check(a, b string) bool {
264
- cnt, n := 0, len(a)
265
248
for i := 0; i < n; i++ {
266
- if a[i] != b[i] {
267
- cnt++
249
+ for j := i + 1; j < n; j++ {
250
+ if check(strs[i], strs[j]) {
251
+ p[find(i)] = find(j)
252
+ }
268
253
}
269
254
}
270
- return cnt <= 2
271
- }
272
-
273
- func find(x int) int {
274
- if p[x] != x {
275
- p[x] = find(p[x])
255
+ ans := 0
256
+ for i := 0; i < n; i++ {
257
+ if i == find(i) {
258
+ ans++
259
+ }
276
260
}
277
- return p[x]
261
+ return ans
278
262
}
279
263
```
280
264
0 commit comments