| | 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 LexicographicallySmallestEquivalentStringAdjacencyMatrix : 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 adjacencyMatrix = new bool[Length, Length]; |
| | 32 | |
|
| 162 | 33 | | for (var i = 0; i < Length; i++) |
| 78 | 34 | | { |
| 78 | 35 | | adjacencyMatrix[i, i] = true; |
| 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 | | adjacencyMatrix[c1, c2] = true; |
| 19 | 44 | | adjacencyMatrix[c2, c1] = true; |
| 19 | 45 | | } |
| | 46 | |
|
| 162 | 47 | | for (var i = 0; i < Length; i++) |
| 78 | 48 | | { |
| 4212 | 49 | | for (var j = 0; j < Length; j++) |
| 2028 | 50 | | { |
| 2028 | 51 | | if (!adjacencyMatrix[i, j]) |
| 1906 | 52 | | { |
| 1906 | 53 | | continue; |
| | 54 | | } |
| | 55 | |
|
| 6588 | 56 | | for (var k = 0; k < Length; k++) |
| 3172 | 57 | | { |
| 3172 | 58 | | if (adjacencyMatrix[i, k]) |
| 274 | 59 | | { |
| 274 | 60 | | adjacencyMatrix[j, k] = true; |
| 274 | 61 | | } |
| 3172 | 62 | | } |
| 122 | 63 | | } |
| 78 | 64 | | } |
| | 65 | |
|
| 3 | 66 | | var resultStringBuilder = new StringBuilder(baseStr.Length); |
| | 67 | |
|
| 49 | 68 | | foreach (var c in baseStr) |
| 20 | 69 | | { |
| 20 | 70 | | var i = c - 'a'; |
| | 71 | |
|
| 226 | 72 | | for (var j = 0; j < Length; j++) |
| 113 | 73 | | { |
| 113 | 74 | | if (!adjacencyMatrix[i, j]) |
| 93 | 75 | | { |
| 93 | 76 | | continue; |
| | 77 | | } |
| | 78 | |
|
| 20 | 79 | | resultStringBuilder.Append((char)(j + 'a')); |
| | 80 | |
|
| 20 | 81 | | break; |
| | 82 | | } |
| 20 | 83 | | } |
| | 84 | |
|
| 3 | 85 | | return resultStringBuilder.ToString(); |
| 3 | 86 | | } |
| | 87 | | } |