A Trie is an efficient tree-like data structure that allows efficient retrieval of a string's prefix scores. We start by inserting each word into the Trie, along with maintaining a count at each node to track how many words share that prefix. Then, for each word, we traverse its prefixes in the Trie to compute the prefix score by collecting the counts stored at each relevant node.
The time complexity is O(n * m) where n is the number of words and m is the maximum length of the words, due to Trie insertions and lookups. The space complexity is O(N * m), N being the total length of all words combined, as each character might lead to a node in the Trie.
1using System;
2using System.Collections.Generic;
3
4class TrieNode {
5 public TrieNode[] Children = new TrieNode[26];
6 public int Count = 0;
7}
8
9class Trie {
10 private TrieNode root;
11 public Trie() {
12 root = new TrieNode();
13 }
14 public void Insert(string word) {
15 TrieNode node = root;
16 foreach (char ch in word) {
17 int index = ch - 'a';
18 if (node.Children[index] == null)
19 node.Children[index] = new TrieNode();
20 node = node.Children[index];
21 node.Count++;
22 }
23 }
24 public int CalculatePrefixScore(string word) {
25 TrieNode node = root;
26 int score = 0;
27 foreach (char ch in word) {
28 node = node.Children[ch - 'a'];
29 score += node.Count;
30 }
31 return score;
32 }
33}
34
35class Solution {
36 public int[] SumOfPrefixScores(string[] words) {
37 Trie trie = new Trie();
38 foreach (string word in words)
39 trie.Insert(word);
40 int[] result = new int[words.Length];
41 for (int i = 0; i < words.Length; i++)
42 result[i] = trie.CalculatePrefixScore(words[i]);
43 return result;
44 }
45 public static void Main() {
46 string[] words = {"abc", "ab", "bc", "b"};
47 int[] result = new Solution().SumOfPrefixScores(words);
48 Console.WriteLine(string.Join(" ", result));
49 }
50}
The C# version employs a Trie structure, utilizing arrays for children nodes. As each word is inserted, counters are increased to record shares in prefixes. For prefix scores, it navigates nodes per word to accumulate node counters into a result.
We can also use a hashtable (or dictionary) to count how often each prefix appears in the list of words. Iterate through each word and generate all its prefixes, updating their counts in the hashtable. Afterwards, compute the sum of counts for each full word by referencing its prefixes in the hashtable.
Time complexity is O(n * m) as we process each prefix per word insertion and retrieval. Space complexity is potentially O(N * m), accounting for hash table storage of prefixes.
1using System;
2using System.Collections.Generic;
3
4class Solution {
5 public int[] SumOfPrefixScores(string[] words) {
6 Dictionary<string, int> prefixCount = new Dictionary<string, int>();
7 foreach (string word in words) {
8 string prefix = "";
9 foreach (char ch in word) {
10 prefix += ch;
11 if (!prefixCount.ContainsKey(prefix))
12 prefixCount[prefix] = 0;
13 prefixCount[prefix]++;
14 }
15 }
16 int[] result = new int[words.Length];
17 for (int i = 0; i < words.Length; i++) {
18 string prefix = "";
19 int sum = 0;
20 foreach (char ch in words[i]) {
21 prefix += ch;
22 sum += prefixCount[prefix];
23 }
24 result[i] = sum;
25 }
26 return result;
27 }
28
29 public static void Main() {
30 string[] words = {"abc", "ab", "bc", "b"};
31 int[] result = new Solution().SumOfPrefixScores(words);
32 Console.WriteLine(string.Join(" ", result));
33 }
34}
In C#, a dictionary encodes prefix occurrences for input words. By fetching these from the dictionary during evaluation of prefix scores, the method efficiently sums counts for insights.