< Summary

Information
Class: LeetCode.Algorithms.ImplementTrie.ImplementTrieArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ImplementTrie\ImplementTrieArray.cs
Line coverage
92%
Covered lines: 24
Uncovered lines: 2
Coverable lines: 26
Total lines: 79
Line coverage: 92.3%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
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%22100%
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\ImplementTrieArray.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 ImplementTrieArray : 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(1)
 22    /// </summary>
 23    /// <param name="word"></param>
 24    public void Insert(string word)
 225    {
 1026        var currentNode = word.Select(c => c - 'a')
 1027            .Aggregate(_root, (current, index) => current.Nodes[index] ??= new Node());
 28
 229        currentNode.IsWord = true;
 230    }
 31
 32    /// <summary>
 33    ///     Time complexity - O(m), where m is the length of the word
 34    ///     Space complexity - O(1)
 35    /// </summary>
 36    /// <param name="word"></param>
 37    /// <returns></returns>
 38    public bool Search(string word)
 339    {
 340        var currentNode = Traverse(word);
 41
 342        return currentNode is { IsWord: true };
 343    }
 44
 45    /// <summary>
 46    ///     Time complexity - O(m), where m is the length of the prefix
 47    ///     Space complexity - O(1)
 48    /// </summary>
 49    /// <param name="prefix"></param>
 50    /// <returns></returns>
 51    public bool StartsWith(string prefix)
 152    {
 153        return Traverse(prefix) != null;
 154    }
 55
 56    private Node? Traverse(string str)
 457    {
 458        var currentNode = _root;
 59
 5460        foreach (var index in str.Select(c => c - 'a'))
 1461        {
 1462            currentNode = currentNode.Nodes[index];
 63
 1464            if (currentNode == null)
 065            {
 066                return null;
 67            }
 1468        }
 69
 470        return currentNode;
 471    }
 72
 73    private class Node
 74    {
 2875        public Node?[] Nodes { get; } = new Node['z' - 'a' + 1];
 76
 577        public bool IsWord { get; set; }
 78    }
 79}