< Summary

Information
Class: LeetCode.Algorithms.VowelSpellchecker.VowelSpellcheckerDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\VowelSpellchecker\VowelSpellcheckerDictionary.cs
Line coverage
100%
Covered lines: 58
Uncovered lines: 0
Coverable lines: 58
Total lines: 111
Line coverage: 100%
Branch coverage
100%
Covered branches: 28
Total branches: 28
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Spellchecker(...)100%1010100%
GetLowercaseWord(...)100%22100%
GetMaskedWord(...)100%44100%
IsVowel(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\VowelSpellchecker\VowelSpellcheckerDictionary.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.VowelSpellchecker;
 13
 14/// <inheritdoc />
 15public sealed class VowelSpellcheckerDictionary : IVowelSpellchecker
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * l), where n is the number of words in wordlist and l is the average word length
 19    ///     Space complexity - O(n * l), where n is the number of words in wordlist and l is the average word length
 20    /// </summary>
 21    /// <param name="wordlist"></param>
 22    /// <param name="queries"></param>
 23    /// <returns></returns>
 24    public string[] Spellchecker(string[] wordlist, string[] queries)
 225    {
 226        var wordsCount = wordlist.Length;
 27
 228        var wordsHashSet = new HashSet<string>(wordsCount);
 229        var lowercaseWordsDictionary = new Dictionary<string, string>(wordsCount);
 230        var maskedWordsDictionary = new Dictionary<string, string>(wordsCount);
 31
 1632        foreach (var word in wordlist)
 533        {
 534            wordsHashSet.Add(word);
 35
 536            var lowercaseWord = GetLowercaseWord(word);
 37
 538            lowercaseWordsDictionary.TryAdd(lowercaseWord, word);
 39
 540            var maskedWord = GetMaskedWord(lowercaseWord);
 41
 542            maskedWordsDictionary.TryAdd(maskedWord, word);
 543        }
 44
 245        var result = new string[queries.Length];
 46
 2647        for (var i = 0; i < queries.Length; i++)
 1148        {
 1149            var query = queries[i];
 50
 1151            if (wordsHashSet.Contains(query))
 352            {
 353                result[i] = query;
 54
 355                continue;
 56            }
 57
 858            var lowercaseWord = GetLowercaseWord(query);
 59
 860            if (lowercaseWordsDictionary.TryGetValue(lowercaseWord, out var capMatch))
 361            {
 362                result[i] = capMatch;
 63
 364                continue;
 65            }
 66
 567            var maskedWord = GetMaskedWord(lowercaseWord);
 68
 569            if (maskedWordsDictionary.TryGetValue(maskedWord, out var vowelMatch))
 270            {
 271                result[i] = vowelMatch;
 272            }
 73            else
 374            {
 375                result[i] = string.Empty;
 376            }
 577        }
 78
 279        return result;
 280    }
 81
 82    private static string GetLowercaseWord(string word)
 1383    {
 1384        return string.Create(word.Length, word, (span, source) =>
 1385        {
 13886            for (var i = 0; i < source.Length; i++)
 5687            {
 5688                span[i] = char.ToLowerInvariant(source[i]);
 5689            }
 2690        });
 1391    }
 92
 93    private static string GetMaskedWord(string word)
 1094    {
 1095        Span<char> buffer = stackalloc char[word.Length];
 96
 10497        for (var i = 0; i < word.Length; i++)
 4298        {
 4299            var c = word[i];
 100
 42101            buffer[i] = IsVowel(c) ? '*' : c;
 42102        }
 103
 10104        return new string(buffer);
 10105    }
 106
 107    private static bool IsVowel(char c)
 42108    {
 42109        return c is 'a' or 'e' or 'i' or 'o' or 'u';
 42110    }
 111}