-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution839.cs
80 lines (71 loc) · 1.84 KB
/
Solution839.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution839
{
/// <summary>
/// 839. Similar String Groups - Hard
/// <a href="https://leetcode.com/problems/similar-string-groups">See the problem</a>
/// </summary>
public int NumSimilarGroups(string[] strs)
{
var n = strs.Length;
// Union-Find data structure
var parent = new int[n];
for (var i = 0; i < n; i++)
{
parent[i] = i;
}
// Find operation with path compression
int Find(int x)
{
if (parent[x] != x)
{
parent[x] = Find(parent[x]);
}
return parent[x];
}
// Union operation
void Union(int x, int y)
{
int rootX = Find(x);
int rootY = Find(y);
if (rootX != rootY)
{
parent[rootX] = rootY;
}
}
// Check if two strings are similar
bool IsSimilar(string a, string b)
{
int diffCount = 0;
for (var i = 0; i < a.Length; i++)
{
if (a[i] != b[i])
{
diffCount++;
if (diffCount > 2) return false;
}
}
return diffCount == 2 || diffCount == 0;
}
// Process all pairs of strings
for (var i = 0; i < n; i++)
{
for (var j = i + 1; j < n; j++)
{
if (IsSimilar(strs[i], strs[j]))
{
Union(i, j);
}
}
}
// Count distinct groups
int groups = 0;
for (int i = 0; i < n; i++)
{
if (Find(i) == i) groups++;
}
return groups;
}
}