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.
1import java.util.*;
2
3class TrieNode {
4 TrieNode[] children = new TrieNode[26];
5 int count = 0;
6}
7
8class Trie {
9 TrieNode root;
10 public Trie() {
11 root = new TrieNode();
12 }
13 public void insert(String word) {
14 TrieNode node = root;
15 for (char ch : word.toCharArray()) {
16 if (node.children[ch - 'a'] == null) {
17 node.children[ch - 'a'] = new TrieNode();
18 }
19 node = node.children[ch - 'a'];
20 node.count++;
21 }
22 }
23 public int calculatePrefixScore(String word) {
24 TrieNode node = root;
25 int score = 0;
26 for (char ch : word.toCharArray()) {
27 node = node.children[ch - 'a'];
28 score += node.count;
29 }
30 return score;
31 }
32}
33
34public class Solution {
35 public int[] sumOfPrefixScores(String[] words) {
36 Trie trie = new Trie();
37 for (String word : words) {
38 trie.insert(word);
39 }
40 int[] result = new int[words.length];
41 for (int i = 0; i < words.length; i++) {
42 result[i] = trie.calculatePrefixScore(words[i]);
43 }
44 return result;
45 }
46 public static void main(String[] args) {
47 String[] words = {"abc", "ab", "bc", "b"};
48 int[] result = new Solution().sumOfPrefixScores(words);
49 System.out.println(Arrays.toString(result));
50 }
51}
In Java, the solution employs a Trie to store the words. During insertion, we increment node counts to track prefix occurrences. For computing prefix scores, we traverse the Trie's nodes corresponding to each word's prefixes, accumulating their counts into a final score.
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.
1def sum_of_prefix_scores(words):
2 prefix_count = {}
3 for word in words:
4 prefix = ''
5 for char in word:
6 prefix += char
7 if prefix in prefix_count:
8 prefix_count[prefix] += 1
9 else:
10 prefix_count[prefix] = 1
11 result = []
12 for word in words:
13 prefix = ''
14 count = 0
15 for char in word:
16 prefix += char
17 count += prefix_count.get(prefix, 0)
18 result.append(count)
19 return result
20
21words = ["abc", "ab", "bc", "b"]
22print(sum_of_prefix_scores(words))
The Python approach makes use of a dictionary to maintain prefix counts. It iteratively processes each word to populate the dictionary, then sums the hit counts for each word's prefixes to determine respective scores.