< Summary

Information
Class: LeetCode.Algorithms.SortVowelsInString.SortVowelsInStringCountingSort
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SortVowelsInString\SortVowelsInStringCountingSort.cs
Line coverage
83%
Covered lines: 36
Uncovered lines: 7
Coverable lines: 43
Total lines: 84
Line coverage: 83.7%
Branch coverage
78%
Covered branches: 30
Total branches: 38
Branch coverage: 78.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SortVowels(...)91.66%1212100%
GetVowelIndex(...)73.07%832656.25%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SortVowelsInString\SortVowelsInStringCountingSort.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.SortVowelsInString;
 13
 14/// <inheritdoc />
 15public sealed class SortVowelsInStringCountingSort : ISortVowelsInString
 16{
 17    private const string VowelOrder = "AEIOUaeiou";
 18
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="s"></param>
 24    /// <returns></returns>
 25    public string SortVowels(string s)
 226    {
 227        Span<int> vowelFrequencies = stackalloc int[VowelOrder.Length];
 28
 3229        foreach (var c in s)
 1330        {
 1331            var vowelIndex = GetVowelIndex(c);
 32
 1333            if (vowelIndex < 0)
 934            {
 935                continue;
 36            }
 37
 438            vowelFrequencies[vowelIndex]++;
 439        }
 40
 241        var sCharArray = s.ToCharArray();
 42
 243        var nextVowelIndex = 0;
 44
 3045        for (var i = 0; i < sCharArray.Length; i++)
 1346        {
 1347            var vowelIndex = GetVowelIndex(sCharArray[i]);
 48
 1349            if (vowelIndex < 0)
 950            {
 951                continue;
 52            }
 53
 1054            while (nextVowelIndex < vowelFrequencies.Length && vowelFrequencies[nextVowelIndex] == 0)
 655            {
 656                nextVowelIndex++;
 657            }
 58
 459            sCharArray[i] = VowelOrder[nextVowelIndex];
 60
 461            vowelFrequencies[nextVowelIndex]--;
 462        }
 63
 264        return new string(sCharArray);
 265    }
 66
 67    private static int GetVowelIndex(char c)
 2668    {
 2669        return c switch
 2670        {
 071            'A' => 0,
 272            'E' => 1,
 073            'I' => 2,
 274            'O' => 3,
 075            'U' => 4,
 076            'a' => 5,
 477            'e' => 6,
 078            'i' => 7,
 079            'o' => 8,
 080            'u' => 9,
 1881            _ => -1
 2682        };
 2683    }
 84}