< Summary

Information
Class: LeetCode.Algorithms.SumOfPrefixScoresOfStrings.SumOfPrefixScoresOfStringsTrie
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SumOfPrefixScoresOfStrings\SumOfPrefixScoresOfStringsTrie.cs
Line coverage
96%
Covered lines: 50
Uncovered lines: 2
Coverable lines: 52
Total lines: 110
Line coverage: 96.1%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SumPrefixScores(...)83.33%6690.47%
.ctor(...)100%11100%
get_Root()100%11100%
AddRange(...)100%22100%
Add(...)100%44100%
.ctor()100%11100%
get_Count()100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SumOfPrefixScoresOfStrings\SumOfPrefixScoresOfStringsTrie.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.SumOfPrefixScoresOfStrings;
 13
 14/// <inheritdoc />
 15public class SumOfPrefixScoresOfStringsTrie : ISumOfPrefixScoresOfStrings
 16{
 17    /// <summary>
 18    ///     Time complexity - O(N * L), where N is the number of words and L is the average length of the words
 19    ///     Space complexity - O(N * L), where N is the number of words and L is the average length of the words
 20    /// </summary>
 21    /// <param name="words"></param>
 22    /// <returns></returns>
 23    public int[] SumPrefixScores(string[] words)
 524    {
 525        var trie = new Trie(words);
 26
 527        var result = new int[words.Length];
 28
 4629        for (var i = 0; i < words.Length; i++)
 1830        {
 1831            var sum = 0;
 32
 1833            var currentTrieNode = trie.Root;
 34
 15235            foreach (var character in words[i])
 4936            {
 4937                var characterTrieNodeIndex = character - 'a';
 38
 4939                var characterTrieNode = currentTrieNode.Children[characterTrieNodeIndex];
 40
 4941                if (characterTrieNode == null)
 042                {
 043                    break;
 44                }
 45
 4946                sum += characterTrieNode.Count;
 47
 4948                currentTrieNode = characterTrieNode;
 4949            }
 50
 1851            result[i] = sum;
 1852        }
 53
 554        return result;
 555    }
 56
 57    private class Trie
 58    {
 559        public Trie(IEnumerable<string> words)
 560        {
 561            AddRange(words);
 562        }
 63
 4164        public TrieNode Root { get; } = new();
 65
 66        private void AddRange(IEnumerable<string> words)
 567        {
 5168            foreach (var word in words)
 1869            {
 1870                Add(word);
 1871            }
 572        }
 73
 74        private void Add(string word)
 1875        {
 1876            var currentTrieNode = Root;
 77
 15278            foreach (var character in word)
 4979            {
 4980                var characterTrieNodeIndex = character - 'a';
 81
 4982                var characterTrieNode = currentTrieNode.Children[characterTrieNodeIndex];
 83
 4984                if (characterTrieNode != null)
 1985                {
 1986                    currentTrieNode = characterTrieNode;
 1987                }
 88                else
 3089                {
 3090                    var newTrieNode = new TrieNode();
 91
 3092                    currentTrieNode.Children[characterTrieNodeIndex] = newTrieNode;
 93
 3094                    currentTrieNode = newTrieNode;
 3095                }
 96
 4997                currentTrieNode.Count++;
 4998            }
 1899        }
 100    }
 101
 102    private class TrieNode
 103    {
 104        private const int ChildrenCount = 'z' - 'a' + 1;
 105
 35106        public readonly TrieNode?[] Children = new TrieNode[ChildrenCount];
 107
 147108        public int Count { get; set; }
 109    }
 110}