| | | 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.FindResultantArrayAfterRemovingAnagrams; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public 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) |
| | 2 | 26 | | { |
| | 2 | 27 | | var result = new List<string>(words.Length) |
| | 2 | 28 | | { |
| | 2 | 29 | | words[0] |
| | 2 | 30 | | }; |
| | | 31 | | |
| | 20 | 32 | | for (var i = 1; i < words.Length; i++) |
| | 8 | 33 | | { |
| | 8 | 34 | | if (AreAnagrams(words[i], result[^1])) |
| | 3 | 35 | | { |
| | 3 | 36 | | continue; |
| | | 37 | | } |
| | | 38 | | |
| | 5 | 39 | | result.Add(words[i]); |
| | 5 | 40 | | } |
| | | 41 | | |
| | 2 | 42 | | return result; |
| | 2 | 43 | | } |
| | | 44 | | |
| | | 45 | | private static bool AreAnagrams(string a, string b) |
| | 8 | 46 | | { |
| | 8 | 47 | | if (a.Length != b.Length) |
| | 1 | 48 | | { |
| | 1 | 49 | | return false; |
| | | 50 | | } |
| | | 51 | | |
| | 7 | 52 | | Span<int> frequencies = stackalloc int[AlphabetLength]; |
| | | 53 | | |
| | 42 | 54 | | for (var i = 0; i < a.Length; i++) |
| | 14 | 55 | | { |
| | 14 | 56 | | var aIndex = GetIndex(a[i]); |
| | | 57 | | |
| | 14 | 58 | | frequencies[aIndex]++; |
| | | 59 | | |
| | 14 | 60 | | var bIndex = GetIndex(b[i]); |
| | | 61 | | |
| | 14 | 62 | | frequencies[bIndex]--; |
| | 14 | 63 | | } |
| | | 64 | | |
| | 182 | 65 | | for (var i = 0; i < AlphabetLength; i++) |
| | 88 | 66 | | { |
| | 88 | 67 | | if (frequencies[i] == 0) |
| | 84 | 68 | | { |
| | 84 | 69 | | continue; |
| | | 70 | | } |
| | | 71 | | |
| | 4 | 72 | | return false; |
| | | 73 | | } |
| | | 74 | | |
| | 3 | 75 | | return true; |
| | 8 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static int GetIndex(char c) |
| | 28 | 79 | | { |
| | 28 | 80 | | return c - 'a'; |
| | 28 | 81 | | } |
| | | 82 | | } |