| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | using System.Text; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.LexicographicallySmallestEquivalentString; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class LexicographicallySmallestEquivalentStringAdjacencyUnionFind : ILexicographicallySmallestEquivalentString |
| | 18 | | { |
| | 19 | | private const int Length = 'z' - 'a' + 1; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Time complexity - O(m + n), where m is the length of s1 and n is the length of baseStr |
| | 23 | | /// Space complexity - O(1) |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="s1"></param> |
| | 26 | | /// <param name="s2"></param> |
| | 27 | | /// <param name="baseStr"></param> |
| | 28 | | /// <returns></returns> |
| | 29 | | public string SmallestEquivalentString(string s1, string s2, string baseStr) |
| 3 | 30 | | { |
| 3 | 31 | | var parent = new int[Length]; |
| | 32 | |
|
| 162 | 33 | | for (var i = 0; i < Length; i++) |
| 78 | 34 | | { |
| 78 | 35 | | parent[i] = i; |
| 78 | 36 | | } |
| | 37 | |
|
| 44 | 38 | | for (var i = 0; i < s1.Length; i++) |
| 19 | 39 | | { |
| 19 | 40 | | var c1 = s1[i] - 'a'; |
| 19 | 41 | | var c2 = s2[i] - 'a'; |
| | 42 | |
|
| 19 | 43 | | Union(c1, c2, parent); |
| 19 | 44 | | } |
| | 45 | |
|
| 3 | 46 | | var resultStringBuilder = new StringBuilder(baseStr.Length); |
| | 47 | |
|
| 49 | 48 | | foreach (var c in baseStr) |
| 20 | 49 | | { |
| 20 | 50 | | var i = Find(c - 'a', parent); |
| | 51 | |
|
| 20 | 52 | | resultStringBuilder.Append((char)(i + 'a')); |
| 20 | 53 | | } |
| | 54 | |
|
| 3 | 55 | | return resultStringBuilder.ToString(); |
| 3 | 56 | | } |
| | 57 | |
|
| | 58 | | private static void Union(int x, int y, int[] parent) |
| 19 | 59 | | { |
| 19 | 60 | | var rootX = Find(x, parent); |
| 19 | 61 | | var rootY = Find(y, parent); |
| | 62 | |
|
| 19 | 63 | | if (rootX == rootY) |
| 2 | 64 | | { |
| 2 | 65 | | return; |
| | 66 | | } |
| | 67 | |
|
| 17 | 68 | | if (rootX < rootY) |
| 13 | 69 | | { |
| 13 | 70 | | parent[rootY] = rootX; |
| 13 | 71 | | } |
| | 72 | | else |
| 4 | 73 | | { |
| 4 | 74 | | parent[rootX] = rootY; |
| 4 | 75 | | } |
| 19 | 76 | | } |
| | 77 | |
|
| | 78 | | private static int Find(int x, int[] parent) |
| 58 | 79 | | { |
| 58 | 80 | | var root = x; |
| | 81 | |
|
| 81 | 82 | | while (parent[root] != root) |
| 23 | 83 | | { |
| 23 | 84 | | root = parent[root]; |
| 23 | 85 | | } |
| | 86 | |
|
| 81 | 87 | | while (x != root) |
| 23 | 88 | | { |
| 23 | 89 | | var p = parent[x]; |
| | 90 | |
|
| 23 | 91 | | parent[x] = root; |
| | 92 | |
|
| 23 | 93 | | x = p; |
| 23 | 94 | | } |
| | 95 | |
|
| 58 | 96 | | return root; |
| 58 | 97 | | } |
| | 98 | | } |