< Summary

Information
Class: LeetCode.Algorithms.ReplaceWords.ReplaceWordsHashSet
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ReplaceWords\ReplaceWordsHashSet.cs
Line coverage
100%
Covered lines: 17
Uncovered lines: 0
Coverable lines: 17
Total lines: 53
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ReplaceWords(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ReplaceWords\ReplaceWordsHashSet.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.ReplaceWords;
 13
 14/// <inheritdoc />
 15public class ReplaceWordsHashSet : IReplaceWords
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n + m + k * L), where n is the number of words in the dictionary, k is the number of wor
 19    ///     the sentence, m is the length of the sentence, and L is the length of the longest word
 20    ///     Space complexity - O(n * L + m), where n is the number of words in the dictionary, m is the length of the se
 21    ///     and L is the length of the longest word
 22    /// </summary>
 23    /// <param name="dictionary"></param>
 24    /// <param name="sentence"></param>
 25    /// <returns></returns>
 26    public string ReplaceWords(IList<string> dictionary, string sentence)
 627    {
 628        var dictionaryHashSet = new HashSet<string>(dictionary);
 29
 630        var words = sentence.Split(' ');
 31
 6832        for (var i = 0; i < words.Length; i++)
 2833        {
 2834            var word = words[i];
 35
 14036            for (var j = 0; j < word.Length; j++)
 6137            {
 6138                var prefix = word[..(j + 1)];
 39
 6140                if (!dictionaryHashSet.Contains(prefix))
 4241                {
 4242                    continue;
 43                }
 44
 1945                words[i] = prefix;
 46
 1947                break;
 48            }
 2849        }
 50
 651        return string.Join(' ', words);
 652    }
 53}