< Summary

Information
Class: LeetCode.Algorithms.ImplementTrie.ImplementTrieDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ImplementTrie\ImplementTrieDictionary.cs
Line coverage
94%
Covered lines: 34
Uncovered lines: 2
Coverable lines: 36
Total lines: 92
Line coverage: 94.4%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Insert(...)100%44100%
Search(...)50%22100%
StartsWith(...)100%11100%
Traverse(...)75%4481.81%
get_Nodes()100%11100%
get_IsWord()100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ImplementTrie\ImplementTrieDictionary.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.ImplementTrie;
 13
 14/// <inheritdoc />
 15public class ImplementTrieDictionary : IImplementTrie
 16{
 117    private readonly Node _root = new();
 18
 19    /// <summary>
 20    ///     Time complexity - O(m), where m is the length of the word
 21    ///     Space complexity - O(m), where m is the length of the word
 22    /// </summary>
 23    /// <param name="word"></param>
 24    public void Insert(string word)
 225    {
 226        var currentNode = _root;
 27
 2228        foreach (var c in word)
 829        {
 830            if (currentNode.Nodes.TryGetValue(c, out var node))
 331            {
 332                currentNode = node;
 333            }
 34            else
 535            {
 536                currentNode.Nodes[c] = new Node();
 37
 538                currentNode = currentNode.Nodes[c];
 539            }
 840        }
 41
 242        currentNode.IsWord = true;
 243    }
 44
 45    /// <summary>
 46    ///     Time complexity - O(m), where m is the length of the word
 47    ///     Space complexity - O(1)
 48    /// </summary>
 49    /// <param name="word"></param>
 50    /// <returns></returns>
 51    public bool Search(string word)
 352    {
 353        var currentNode = Traverse(word);
 54
 355        return currentNode is { IsWord: true };
 356    }
 57
 58    /// <summary>
 59    ///     Time complexity - O(m), where m is the length of the prefix
 60    ///     Space complexity - O(1)
 61    /// </summary>
 62    /// <param name="prefix"></param>
 63    /// <returns></returns>
 64    public bool StartsWith(string prefix)
 165    {
 166        return Traverse(prefix) != null;
 167    }
 68
 69    private Node? Traverse(string str)
 470    {
 471        var currentNode = _root;
 72
 4073        foreach (var c in str)
 1474        {
 1475            if (!currentNode.Nodes.TryGetValue(c, out var node))
 076            {
 077                return null;
 78            }
 79
 1480            currentNode = node;
 1481        }
 82
 483        return currentNode;
 484    }
 85
 86    private class Node
 87    {
 3888        public Dictionary<char, Node> Nodes { get; } = new();
 89
 590        public bool IsWord { get; set; }
 91    }
 92}