| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.SumOfPrefixScoresOfStrings; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public 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) |
| 6 | 24 | | { |
| 6 | 25 | | var trie = new Trie(words); |
| | 26 | |
|
| 6 | 27 | | var result = new int[words.Length]; |
| | 28 | |
|
| 64 | 29 | | for (var i = 0; i < words.Length; i++) |
| 26 | 30 | | { |
| 26 | 31 | | var sum = 0; |
| | 32 | |
|
| 26 | 33 | | var currentTrieNode = trie.Root; |
| | 34 | |
|
| 236 | 35 | | foreach (var character in words[i]) |
| 79 | 36 | | { |
| 79 | 37 | | var characterTrieNodeIndex = character - 'a'; |
| | 38 | |
|
| 79 | 39 | | var characterTrieNode = currentTrieNode.Children[characterTrieNodeIndex]; |
| | 40 | |
|
| 79 | 41 | | if (characterTrieNode == null) |
| 0 | 42 | | { |
| 0 | 43 | | break; |
| | 44 | | } |
| | 45 | |
|
| 79 | 46 | | sum += characterTrieNode.Count; |
| | 47 | |
|
| 79 | 48 | | currentTrieNode = characterTrieNode; |
| 79 | 49 | | } |
| | 50 | |
|
| 26 | 51 | | result[i] = sum; |
| 26 | 52 | | } |
| | 53 | |
|
| 6 | 54 | | return result; |
| 6 | 55 | | } |
| | 56 | |
|
| | 57 | | private class Trie |
| | 58 | | { |
| 6 | 59 | | public Trie(IEnumerable<string> words) |
| 6 | 60 | | { |
| 6 | 61 | | AddRange(words); |
| 6 | 62 | | } |
| | 63 | |
|
| 58 | 64 | | public TrieNode Root { get; } = new(); |
| | 65 | |
|
| | 66 | | private void AddRange(IEnumerable<string> words) |
| 6 | 67 | | { |
| 70 | 68 | | foreach (var word in words) |
| 26 | 69 | | { |
| 26 | 70 | | Add(word); |
| 26 | 71 | | } |
| 6 | 72 | | } |
| | 73 | |
|
| | 74 | | private void Add(string word) |
| 26 | 75 | | { |
| 26 | 76 | | var currentTrieNode = Root; |
| | 77 | |
|
| 236 | 78 | | foreach (var character in word) |
| 79 | 79 | | { |
| 79 | 80 | | var characterTrieNodeIndex = character - 'a'; |
| | 81 | |
|
| 79 | 82 | | var characterTrieNode = currentTrieNode.Children[characterTrieNodeIndex]; |
| | 83 | |
|
| 79 | 84 | | if (characterTrieNode != null) |
| 33 | 85 | | { |
| 33 | 86 | | currentTrieNode = characterTrieNode; |
| 33 | 87 | | } |
| | 88 | | else |
| 46 | 89 | | { |
| 46 | 90 | | var newTrieNode = new TrieNode(); |
| | 91 | |
|
| 46 | 92 | | currentTrieNode.Children[characterTrieNodeIndex] = newTrieNode; |
| | 93 | |
|
| 46 | 94 | | currentTrieNode = newTrieNode; |
| 46 | 95 | | } |
| | 96 | |
|
| 79 | 97 | | currentTrieNode.Count++; |
| 79 | 98 | | } |
| 26 | 99 | | } |
| | 100 | | } |
| | 101 | |
|
| | 102 | | private class TrieNode |
| | 103 | | { |
| | 104 | | private const int ChildrenCount = 'z' - 'a' + 1; |
| | 105 | |
|
| 52 | 106 | | public readonly TrieNode?[] Children = new TrieNode[ChildrenCount]; |
| | 107 | |
|
| 237 | 108 | | public int Count { get; set; } |
| | 109 | | } |
| | 110 | | } |