| | 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.CountOfSubstringsContainingEveryVowelAndKConsonants2; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class CountOfSubstringsContainingEveryVowelAndKConsonants2SlidingWindow : |
| | 16 | | ICountOfSubstringsContainingEveryVowelAndKConsonants2 |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Time complexity - O(n) |
| | 20 | | /// Space complexity - O(1) |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="word"></param> |
| | 23 | | /// <param name="k"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public long CountOfSubstrings(string word, int k) |
| 4 | 26 | | { |
| 4 | 27 | | return AtLeastK(word, k) - AtLeastK(word, k + 1); |
| 4 | 28 | | } |
| | 29 | |
|
| | 30 | | private static long AtLeastK(string word, int k) |
| 8 | 31 | | { |
| 8 | 32 | | long count = 0; |
| | 33 | |
|
| 8 | 34 | | var start = 0; |
| 8 | 35 | | var end = 0; |
| | 36 | |
|
| 8 | 37 | | var vowelDictionary = new Dictionary<char, int>(); |
| 8 | 38 | | var consonantCount = 0; |
| | 39 | |
|
| 74 | 40 | | while (end < word.Length) |
| 66 | 41 | | { |
| 66 | 42 | | var newLetter = word[end]; |
| | 43 | |
|
| 66 | 44 | | if (IsVowel(newLetter)) |
| 50 | 45 | | { |
| 50 | 46 | | vowelDictionary.TryAdd(newLetter, 0); |
| | 47 | |
|
| 50 | 48 | | vowelDictionary[newLetter]++; |
| 50 | 49 | | } |
| | 50 | | else |
| 16 | 51 | | { |
| 16 | 52 | | consonantCount++; |
| 16 | 53 | | } |
| | 54 | |
|
| 85 | 55 | | while (vowelDictionary.Count == 5 && consonantCount >= k) |
| 19 | 56 | | { |
| 19 | 57 | | count += word.Length - end; |
| | 58 | |
|
| 19 | 59 | | var startLetter = word[start]; |
| | 60 | |
|
| 19 | 61 | | if (IsVowel(startLetter)) |
| 14 | 62 | | { |
| 14 | 63 | | vowelDictionary[startLetter]--; |
| | 64 | |
|
| 14 | 65 | | if (vowelDictionary[startLetter] == 0) |
| 14 | 66 | | { |
| 14 | 67 | | vowelDictionary.Remove(startLetter); |
| 14 | 68 | | } |
| 14 | 69 | | } |
| | 70 | | else |
| 5 | 71 | | { |
| 5 | 72 | | consonantCount--; |
| 5 | 73 | | } |
| | 74 | |
|
| 19 | 75 | | start++; |
| 19 | 76 | | } |
| | 77 | |
|
| 66 | 78 | | end++; |
| 66 | 79 | | } |
| | 80 | |
|
| 8 | 81 | | return count; |
| 8 | 82 | | } |
| | 83 | |
|
| | 84 | | private static bool IsVowel(char c) |
| 85 | 85 | | { |
| 85 | 86 | | return c is 'a' or 'e' or 'i' or 'o' or 'u'; |
| 85 | 87 | | } |
| | 88 | | } |