< Summary

Information
Class: LeetCode.Algorithms.CountOfSubstringsContainingEveryVowelAndKConsonants2.CountOfSubstringsContainingEveryVowelAndKConsonants2SlidingWindow
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountOfSubstringsContainingEveryVowelAndKConsonants2\CountOfSubstringsContainingEveryVowelAndKConsonants2SlidingWindow.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 88
Line coverage: 100%
Branch coverage
100%
Covered branches: 24
Total branches: 24
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountOfSubstrings(...)100%11100%
AtLeastK(...)100%1212100%
IsVowel(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountOfSubstringsContainingEveryVowelAndKConsonants2\CountOfSubstringsContainingEveryVowelAndKConsonants2SlidingWindow.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.CountOfSubstringsContainingEveryVowelAndKConsonants2;
 13
 14/// <inheritdoc />
 15public 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)
 426    {
 427        return AtLeastK(word, k) - AtLeastK(word, k + 1);
 428    }
 29
 30    private static long AtLeastK(string word, int k)
 831    {
 832        long count = 0;
 33
 834        var start = 0;
 835        var end = 0;
 36
 837        var vowelDictionary = new Dictionary<char, int>();
 838        var consonantCount = 0;
 39
 7440        while (end < word.Length)
 6641        {
 6642            var newLetter = word[end];
 43
 6644            if (IsVowel(newLetter))
 5045            {
 5046                vowelDictionary.TryAdd(newLetter, 0);
 47
 5048                vowelDictionary[newLetter]++;
 5049            }
 50            else
 1651            {
 1652                consonantCount++;
 1653            }
 54
 8555            while (vowelDictionary.Count == 5 && consonantCount >= k)
 1956            {
 1957                count += word.Length - end;
 58
 1959                var startLetter = word[start];
 60
 1961                if (IsVowel(startLetter))
 1462                {
 1463                    vowelDictionary[startLetter]--;
 64
 1465                    if (vowelDictionary[startLetter] == 0)
 1466                    {
 1467                        vowelDictionary.Remove(startLetter);
 1468                    }
 1469                }
 70                else
 571                {
 572                    consonantCount--;
 573                }
 74
 1975                start++;
 1976            }
 77
 6678            end++;
 6679        }
 80
 881        return count;
 882    }
 83
 84    private static bool IsVowel(char c)
 8585    {
 8586        return c is 'a' or 'e' or 'i' or 'o' or 'u';
 8587    }
 88}