< Summary

Information
Class: LeetCode.Algorithms.ReplaceWords.ReplaceWordsTrieNode
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ReplaceWords\ReplaceWordsTrieNode.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 95
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
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%88100%
.ctor(...)100%11100%
get_Root()100%11100%
Add(...)100%44100%
AddRange(...)100%22100%
get_Children()100%11100%
get_Word()100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ReplaceWords\ReplaceWordsTrieNode.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 ReplaceWordsTrieNode : IReplaceWords
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * L + k * L), where n is the number of words in the dictionary, L is the length of the
 19    ///     k is the number of words in the sentence
 20    ///     Space complexity - O(n * L + m), where n is the number of words in the dictionary, L is the length of the wo
 21    ///     is the length of the sentence
 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 trie = new Trie(dictionary);
 29
 630        var words = sentence.Split(' ');
 31
 6832        for (var i = 0; i < words.Length; i++)
 2833        {
 2834            var word = words[i];
 35
 2836            var current = trie.Root;
 37
 21938            foreach (var c in word.TakeWhile(c => current.Children.ContainsKey(c) && current.Word == null))
 3739            {
 3740                current = current.Children[c];
 3741            }
 42
 2843            if (current.Word != null)
 1944            {
 1945                words[i] = current.Word;
 1946            }
 2847        }
 48
 649        return string.Join(' ', words);
 650    }
 51
 52    private class Trie
 53    {
 654        public Trie(IEnumerable<string> words)
 655        {
 656            AddRange(words);
 657        }
 58
 5159        public TrieNode Root { get; } = new();
 60
 61        private void Add(string word)
 1762        {
 1763            var node = Root;
 64
 15965            foreach (var c in word)
 5466            {
 5467                if (!node.Children.TryGetValue(c, out var value))
 3568                {
 3569                    value = new TrieNode();
 70
 3571                    node.Children[c] = value;
 3572                }
 73
 5474                node = value;
 5475            }
 76
 1777            node.Word = word;
 1778        }
 79
 80        private void AddRange(IEnumerable<string> words)
 681        {
 5282            foreach (var word in words)
 1783            {
 1784                Add(word);
 1785            }
 686        }
 87    }
 88
 89    private class TrieNode
 90    {
 22891        public Dictionary<char, TrieNode> Children { get; } = [];
 92
 10693        public string? Word { get; set; }
 94    }
 95}