< Summary

Information
Class: LeetCode.Algorithms.FindResultantArrayAfterRemovingAnagrams.FindResultantArrayAfterRemovingAnagramsGreedyCounting
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindResultantArrayAfterRemovingAnagrams\FindResultantArrayAfterRemovingAnagramsGreedyCounting.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 82
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RemoveAnagrams(...)100%44100%
AreAnagrams(...)100%88100%
GetIndex(...)100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindResultantArrayAfterRemovingAnagrams\FindResultantArrayAfterRemovingAnagramsGreedyCounting.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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
 12namespace LeetCode.Algorithms.FindResultantArrayAfterRemovingAnagrams;
 13
 14/// <inheritdoc />
 15public sealed class FindResultantArrayAfterRemovingAnagramsGreedyCounting : IFindResultantArrayAfterRemovingAnagrams
 16{
 17    private const byte AlphabetLength = 'z' - 'a' + 1;
 18
 19    /// <summary>
 20    ///     Time complexity - O(n * L), where n is the number of words and L is the average length of a word
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="words"></param>
 24    /// <returns></returns>
 25    public IList<string> RemoveAnagrams(string[] words)
 226    {
 227        var result = new List<string>(words.Length)
 228        {
 229            words[0]
 230        };
 31
 2032        for (var i = 1; i < words.Length; i++)
 833        {
 834            if (AreAnagrams(words[i], result[^1]))
 335            {
 336                continue;
 37            }
 38
 539            result.Add(words[i]);
 540        }
 41
 242        return result;
 243    }
 44
 45    private static bool AreAnagrams(string a, string b)
 846    {
 847        if (a.Length != b.Length)
 148        {
 149            return false;
 50        }
 51
 752        Span<int> frequencies = stackalloc int[AlphabetLength];
 53
 4254        for (var i = 0; i < a.Length; i++)
 1455        {
 1456            var aIndex = GetIndex(a[i]);
 57
 1458            frequencies[aIndex]++;
 59
 1460            var bIndex = GetIndex(b[i]);
 61
 1462            frequencies[bIndex]--;
 1463        }
 64
 18265        for (var i = 0; i < AlphabetLength; i++)
 8866        {
 8867            if (frequencies[i] == 0)
 8468            {
 8469                continue;
 70            }
 71
 472            return false;
 73        }
 74
 375        return true;
 876    }
 77
 78    private static int GetIndex(char c)
 2879    {
 2880        return c - 'a';
 2881    }
 82}