| | 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.MostCommonWord; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public 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) |
| 3 | 27 | | { |
| 3 | 28 | | var mostCommonWord = string.Empty; |
| 3 | 29 | | var mostFrequency = 0; |
| | 30 | |
|
| 3 | 31 | | var wordsFrequencyDictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); |
| 3 | 32 | | var bannedHashSet = new HashSet<string>(banned, StringComparer.OrdinalIgnoreCase); |
| | 33 | |
|
| 3 | 34 | | var currentWordStringBuilder = new StringBuilder(); |
| | 35 | |
|
| 167 | 36 | | foreach (var character in paragraph) |
| 79 | 37 | | { |
| 79 | 38 | | if (char.IsLetter(character)) |
| 51 | 39 | | { |
| 51 | 40 | | currentWordStringBuilder.Append(char.ToLowerInvariant(character)); |
| 51 | 41 | | } |
| 28 | 42 | | else if (currentWordStringBuilder.Length > 0) |
| 22 | 43 | | { |
| 22 | 44 | | ProcessWord(currentWordStringBuilder, bannedHashSet, wordsFrequencyDictionary, ref mostCommonWord, |
| 22 | 45 | | ref mostFrequency); |
| 22 | 46 | | } |
| 79 | 47 | | } |
| | 48 | |
|
| 3 | 49 | | if (currentWordStringBuilder.Length > 0) |
| 1 | 50 | | { |
| 1 | 51 | | ProcessWord(currentWordStringBuilder, bannedHashSet, wordsFrequencyDictionary, ref mostCommonWord, |
| 1 | 52 | | ref mostFrequency); |
| 1 | 53 | | } |
| | 54 | |
|
| 3 | 55 | | return mostCommonWord; |
| 3 | 56 | | } |
| | 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) |
| 23 | 63 | | { |
| 23 | 64 | | var word = currentWordStringBuilder.ToString(); |
| | 65 | |
|
| 23 | 66 | | currentWordStringBuilder.Clear(); |
| | 67 | |
|
| 23 | 68 | | if (bannedHashSet.Contains(word)) |
| 7 | 69 | | { |
| 7 | 70 | | return; |
| | 71 | | } |
| | 72 | |
|
| 16 | 73 | | if (!wordsFrequencyDictionary.TryAdd(word, 1)) |
| 4 | 74 | | { |
| 4 | 75 | | wordsFrequencyDictionary[word]++; |
| 4 | 76 | | } |
| | 77 | |
|
| 16 | 78 | | if (wordsFrequencyDictionary[word] <= mostFrequency) |
| 10 | 79 | | { |
| 10 | 80 | | return; |
| | 81 | | } |
| | 82 | |
|
| 6 | 83 | | mostFrequency = wordsFrequencyDictionary[word]; |
| 6 | 84 | | mostCommonWord = word; |
| 23 | 85 | | } |
| | 86 | | } |