< Summary

Information
Class: LeetCode.Algorithms.FindCommonCharacters.FindCommonCharactersDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindCommonCharacters\FindCommonCharactersDictionary.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 58
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
CommonChars(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindCommonCharacters\FindCommonCharactersDictionary.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
 12namespace LeetCode.Algorithms.FindCommonCharacters;
 13
 14/// <inheritdoc />
 15public class FindCommonCharactersDictionary : IFindCommonCharacters
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * m), where n is the number of words and m is the maximum length of the words
 19    ///     Space complexity - O(m), m is the maximum length of the words
 20    /// </summary>
 21    /// <param name="words"></param>
 22    /// <returns></returns>
 23    public IList<string> CommonChars(string[] words)
 924    {
 925        var commonChars = words[0].ToList();
 26
 6527        foreach (var word in words.Skip(1))
 1928        {
 1929            var wordDictionary = new Dictionary<char, int>();
 30
 21431            foreach (var c in word.Where(c => !wordDictionary.TryAdd(c, 1)))
 3432            {
 3433                wordDictionary[c]++;
 3434            }
 35
 1936            var charsToRemove = new List<char>();
 37
 15538            foreach (var c in commonChars)
 4939            {
 4940                if (wordDictionary.TryGetValue(c, out var count) && count > 0)
 3541                {
 3542                    wordDictionary[c]--;
 3543                }
 44                else
 1445                {
 1446                    charsToRemove.Add(c);
 1447                }
 4948            }
 49
 8550            foreach (var charToRemove in charsToRemove)
 1451            {
 1452                commonChars.Remove(charToRemove);
 1453            }
 1954        }
 55
 2556        return commonChars.Select(c => c.ToString()).ToList();
 957    }
 58}

Methods/Properties

CommonChars(System.String[])