< Summary

Information
Class: LeetCode.Algorithms.MostCommonWord.MostCommonWordDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MostCommonWord\MostCommonWordDictionary.cs
Line coverage
100%
Covered lines: 41
Uncovered lines: 0
Coverable lines: 41
Total lines: 86
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
MostCommonWord(...)100%88100%
ProcessWord(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MostCommonWord\MostCommonWordDictionary.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.MostCommonWord;
 15
 16/// <inheritdoc />
 17public class MostCommonWordDictionary : IMostCommonWord
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="paragraph"></param>
 24    /// <param name="banned"></param>
 25    /// <returns></returns>
 26    public string MostCommonWord(string paragraph, string[] banned)
 327    {
 328        var mostCommonWord = string.Empty;
 329        var mostFrequency = 0;
 30
 331        var wordsFrequencyDictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
 332        var bannedHashSet = new HashSet<string>(banned, StringComparer.OrdinalIgnoreCase);
 33
 334        var currentWordStringBuilder = new StringBuilder();
 35
 16736        foreach (var character in paragraph)
 7937        {
 7938            if (char.IsLetter(character))
 5139            {
 5140                currentWordStringBuilder.Append(char.ToLowerInvariant(character));
 5141            }
 2842            else if (currentWordStringBuilder.Length > 0)
 2243            {
 2244                ProcessWord(currentWordStringBuilder, bannedHashSet, wordsFrequencyDictionary, ref mostCommonWord,
 2245                    ref mostFrequency);
 2246            }
 7947        }
 48
 349        if (currentWordStringBuilder.Length > 0)
 150        {
 151            ProcessWord(currentWordStringBuilder, bannedHashSet, wordsFrequencyDictionary, ref mostCommonWord,
 152                ref mostFrequency);
 153        }
 54
 355        return mostCommonWord;
 356    }
 57
 58    private static void ProcessWord(StringBuilder currentWordStringBuilder,
 59        HashSet<string> bannedHashSet,
 60        Dictionary<string, int> wordsFrequencyDictionary,
 61        ref string mostCommonWord,
 62        ref int mostFrequency)
 2363    {
 2364        var word = currentWordStringBuilder.ToString();
 65
 2366        currentWordStringBuilder.Clear();
 67
 2368        if (bannedHashSet.Contains(word))
 769        {
 770            return;
 71        }
 72
 1673        if (!wordsFrequencyDictionary.TryAdd(word, 1))
 474        {
 475            wordsFrequencyDictionary[word]++;
 476        }
 77
 1678        if (wordsFrequencyDictionary[word] <= mostFrequency)
 1079        {
 1080            return;
 81        }
 82
 683        mostFrequency = wordsFrequencyDictionary[word];
 684        mostCommonWord = word;
 2385    }
 86}