| | 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 | | namespace LeetCode.Algorithms.FindMostFrequentVowelAndConsonant; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FindMostFrequentVowelAndConsonantFrequencyArray : IFindMostFrequentVowelAndConsonant |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="s"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int MaxFreqSum(string s) |
| 2 | 24 | | { |
| 2 | 25 | | var frequencyArray = new char['z' - 'a' + 1]; |
| | 26 | |
|
| 2 | 27 | | var maxVowel = 0; |
| 2 | 28 | | var maxConsonant = 0; |
| | 29 | |
|
| 38 | 30 | | foreach (var c in s) |
| 16 | 31 | | { |
| 16 | 32 | | var index = c - 'a'; |
| | 33 | |
|
| 16 | 34 | | frequencyArray[index]++; |
| | 35 | |
|
| 16 | 36 | | if (IsVowel(c)) |
| 10 | 37 | | { |
| 10 | 38 | | maxVowel = Math.Max(maxVowel, frequencyArray[index]); |
| 10 | 39 | | } |
| | 40 | | else |
| 6 | 41 | | { |
| 6 | 42 | | maxConsonant = Math.Max(maxConsonant, frequencyArray[index]); |
| 6 | 43 | | } |
| 16 | 44 | | } |
| | 45 | |
|
| 2 | 46 | | return maxVowel + maxConsonant; |
| 2 | 47 | | } |
| | 48 | |
|
| | 49 | | private static bool IsVowel(char c) |
| 16 | 50 | | { |
| 16 | 51 | | return c is 'a' or 'e' or 'i' or 'o' or 'u'; |
| 16 | 52 | | } |
| | 53 | | } |