< Summary

Information
Class: LeetCode.Algorithms.LexicographicallySmallestEquivalentString.LexicographicallySmallestEquivalentStringAdjacencyUnionFind
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LexicographicallySmallestEquivalentString\LexicographicallySmallestEquivalentStringAdjacencyUnionFind.cs
Line coverage
100%
Covered lines: 48
Uncovered lines: 0
Coverable lines: 48
Total lines: 98
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SmallestEquivalentString(...)100%66100%
Union(...)100%44100%
Find(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LexicographicallySmallestEquivalentString\LexicographicallySmallestEquivalentStringAdjacencyUnionFind.cs

#LineLine coverage
 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
 12using System.Text;
 13
 14namespace LeetCode.Algorithms.LexicographicallySmallestEquivalentString;
 15
 16/// <inheritdoc />
 17public 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)
 330    {
 331        var parent = new int[Length];
 32
 16233        for (var i = 0; i < Length; i++)
 7834        {
 7835            parent[i] = i;
 7836        }
 37
 4438        for (var i = 0; i < s1.Length; i++)
 1939        {
 1940            var c1 = s1[i] - 'a';
 1941            var c2 = s2[i] - 'a';
 42
 1943            Union(c1, c2, parent);
 1944        }
 45
 346        var resultStringBuilder = new StringBuilder(baseStr.Length);
 47
 4948        foreach (var c in baseStr)
 2049        {
 2050            var i = Find(c - 'a', parent);
 51
 2052            resultStringBuilder.Append((char)(i + 'a'));
 2053        }
 54
 355        return resultStringBuilder.ToString();
 356    }
 57
 58    private static void Union(int x, int y, int[] parent)
 1959    {
 1960        var rootX = Find(x, parent);
 1961        var rootY = Find(y, parent);
 62
 1963        if (rootX == rootY)
 264        {
 265            return;
 66        }
 67
 1768        if (rootX < rootY)
 1369        {
 1370            parent[rootY] = rootX;
 1371        }
 72        else
 473        {
 474            parent[rootX] = rootY;
 475        }
 1976    }
 77
 78    private static int Find(int x, int[] parent)
 5879    {
 5880        var root = x;
 81
 8182        while (parent[root] != root)
 2383        {
 2384            root = parent[root];
 2385        }
 86
 8187        while (x != root)
 2388        {
 2389            var p = parent[x];
 90
 2391            parent[x] = root;
 92
 2393            x = p;
 2394        }
 95
 5896        return root;
 5897    }
 98}