-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1061.cs
68 lines (58 loc) · 1.71 KB
/
Solution1061.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
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1061
{
private readonly int[] parent = new int[26];
/// <summary>
/// 1061. Lexicographically Smallest Equivalent String - Medium
/// <a href="https://leetcode.com/problems/lexicographically-smallest-equivalent-string"</a>
/// </summary>
public string SmallestEquivalentString(string s1, string s2, string baseStr)
{
// Initialize Union-Find structure
for (int i = 0; i < 26; i++)
{
parent[i] = i;
}
// Union operation to group equivalent characters
for (int i = 0; i < s1.Length; i++)
{
int char1 = s1[i] - 'a';
int char2 = s2[i] - 'a';
Union(char1, char2);
}
// Transform baseStr using the smallest equivalent characters
char[] result = new char[baseStr.Length];
for (int i = 0; i < baseStr.Length; i++)
{
int root = Find(baseStr[i] - 'a');
result[i] = (char)('a' + root);
}
return new string(result);
}
// Find operation with path compression for efficiency
private int Find(int x)
{
if (parent[x] != x)
{
parent[x] = Find(parent[x]); // Path compression
}
return parent[x];
}
// Union operation to merge two groups
private void Union(int x, int y)
{
int rootX = Find(x);
int rootY = Find(y);
// Assign the smaller character as the parent for lexicographical order
if (rootX < rootY)
{
parent[rootY] = rootX;
}
else
{
parent[rootX] = rootY;
}
}
}