< 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)
 624    {
 625        var trie = new Trie(words);
 26
 627        var result = new int[words.Length];
 28
 6429        for (var i = 0; i < words.Length; i++)
 2630        {
 2631            var sum = 0;
 32
 2633            var currentTrieNode = trie.Root;
 34
 23635            foreach (var character in words[i])
 7936            {
 7937                var characterTrieNodeIndex = character - 'a';
 38
 7939                var characterTrieNode = currentTrieNode.Children[characterTrieNodeIndex];
 40
 7941                if (characterTrieNode == null)
 042                {
 043                    break;
 44                }
 45
 7946                sum += characterTrieNode.Count;
 47
 7948                currentTrieNode = characterTrieNode;
 7949            }
 50
 2651            result[i] = sum;
 2652        }
 53
 654        return result;
 655    }
 56
 57    private class Trie
 58    {
 659        public Trie(IEnumerable<string> words)
 660        {
 661            AddRange(words);
 662        }
 63
 5864        public TrieNode Root { get; } = new();
 65
 66        private void AddRange(IEnumerable<string> words)
 667        {
 7068            foreach (var word in words)
 2669            {
 2670                Add(word);
 2671            }
 672        }
 73
 74        private void Add(string word)
 2675        {
 2676            var currentTrieNode = Root;
 77
 23678            foreach (var character in word)
 7979            {
 7980                var characterTrieNodeIndex = character - 'a';
 81
 7982                var characterTrieNode = currentTrieNode.Children[characterTrieNodeIndex];
 83
 7984                if (characterTrieNode != null)
 3385                {
 3386                    currentTrieNode = characterTrieNode;
 3387                }
 88                else
 4689                {
 4690                    var newTrieNode = new TrieNode();
 91
 4692                    currentTrieNode.Children[characterTrieNodeIndex] = newTrieNode;
 93
 4694                    currentTrieNode = newTrieNode;
 4695                }
 96
 7997                currentTrieNode.Count++;
 7998            }
 2699        }
 100    }
 101
 102    private class TrieNode
 103    {
 104        private const int ChildrenCount = 'z' - 'a' + 1;
 105
 52106        public readonly TrieNode?[] Children = new TrieNode[ChildrenCount];
 107
 237108        public int Count { get; set; }
 109    }
 110}