| | | 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.FindCommonCharacters; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class FindCommonCharactersArray : 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(n * m), where n is the number of words and 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) |
| | 9 | 24 | | { |
| | 9 | 25 | | var minFrequency = new int[26]; |
| | | 26 | | |
| | 9 | 27 | | Array.Fill(minFrequency, int.MaxValue); |
| | | 28 | | |
| | 83 | 29 | | foreach (var word in words) |
| | 28 | 30 | | { |
| | 28 | 31 | | var charCount = new int[26]; |
| | | 32 | | |
| | 322 | 33 | | foreach (var c in word) |
| | 119 | 34 | | { |
| | 119 | 35 | | charCount[c - 'a']++; |
| | 119 | 36 | | } |
| | | 37 | | |
| | 1512 | 38 | | for (var i = 0; i < 26; i++) |
| | 728 | 39 | | { |
| | 728 | 40 | | minFrequency[i] = Math.Min(minFrequency[i], charCount[i]); |
| | 728 | 41 | | } |
| | 28 | 42 | | } |
| | | 43 | | |
| | 9 | 44 | | var commonChars = new List<string>(); |
| | | 45 | | |
| | 486 | 46 | | for (var i = 0; i < 26; i++) |
| | 234 | 47 | | { |
| | 500 | 48 | | for (var j = 0; j < minFrequency[i]; j++) |
| | 16 | 49 | | { |
| | 16 | 50 | | commonChars.Add(((char)(i + 'a')).ToString()); |
| | 16 | 51 | | } |
| | 234 | 52 | | } |
| | | 53 | | |
| | 9 | 54 | | return commonChars; |
| | 9 | 55 | | } |
| | | 56 | | } |