This project demonstrates the implementation of a Trie (prefix tree) data structure to solve the problem of calculating the sum of prefix scores for a list of words. The Trie is used to efficiently store and retrieve prefixes for each word in the input list, and the prefix score is calculated as the number of words that share a common prefix.
- TrieNode: A node in the Trie that stores child nodes and a count to track how many words pass through this node (i.e., how many words share this prefix).
- Trie: A class that implements a Trie with the ability to insert words and calculate prefix scores.
-
Define a TrieNode Structure:
- Contains a map to store child nodes, where each character points to the next node.
count
tracks how many words pass through this node (or prefix).
-
Define a Trie Class:
- The Trie contains the root node and supports word insertion and prefix score calculation.
-
Insert Words into the Trie:
- For each word, traverse the Trie character by character.
- Create a new node if a character is missing and update the
count
for each node passed.
-
Calculate Prefix Scores:
- For each word, traverse the Trie again and sum the
count
values for each character in the word, which represents how many words share the prefix up to that character.
- For each word, traverse the Trie again and sum the
-
Solution Class:
- Insert all words into the Trie.
- For each word, calculate the prefix score and return the results in an array.
-
Define a TrieNode Class:
- Contains a HashMap for storing child nodes (characters) and a
count
variable to track how many words pass through the node.
- Contains a HashMap for storing child nodes (characters) and a
-
Define a Trie Class:
- Contains the root node and methods to insert words and calculate prefix scores.
-
Insert Words:
- Traverse each character of the word, create a new TrieNode if it doesn't exist, and increment the
count
for each node passed.
- Traverse each character of the word, create a new TrieNode if it doesn't exist, and increment the
-
Calculate Prefix Scores:
- For each word, traverse its characters and sum the
count
values at each step to compute the total score for all prefixes of the word.
- For each word, traverse its characters and sum the
-
Solution Class:
- Insert words into the Trie and then compute and return the prefix scores for each word in the input list.
-
Define a TrieNode Class:
children
stores the next characters as child nodes, andcount
keeps track of how many words pass through the node.
-
Define a Trie Class:
- The Trie class holds the root node and methods to insert words and compute prefix scores.
-
Insert Words:
- Traverse the word, and if a character doesn't exist as a child, create a new TrieNode.
- Increment the
count
for each node while inserting the word.
-
Calculate Prefix Scores:
- For each word, traverse the Trie character by character, summing the
count
at each node to calculate the total score for the word's prefixes.
- For each word, traverse the Trie character by character, summing the
-
Function to Compute Scores:
- Insert all words into the Trie and calculate prefix scores, storing them in an array and returning it.
-
Define a TrieNode Class:
children
is a dictionary to store child nodes, andcount
tracks how many words pass through each prefix.
-
Define a Trie Class:
- Contains the root node and methods to insert words and calculate the prefix score.
-
Insert Words:
- For each word, traverse character by character, creating new TrieNodes if necessary and incrementing the
count
for each node.
- For each word, traverse character by character, creating new TrieNodes if necessary and incrementing the
-
Calculate Prefix Scores:
- For each word, traverse its characters and sum the
count
values at each step to calculate the total score for all its prefixes.
- For each word, traverse its characters and sum the
-
Solution Class:
- Insert words into the Trie, then compute and return the prefix scores for each word in the list.
-
Define a TrieNode Struct:
- Contains a map to store child nodes (
children
) and acount
variable to track how many words pass through each prefix.
- Contains a map to store child nodes (
-
Define a Trie Struct:
- The Trie contains the root node and methods for inserting words and calculating prefix scores.
-
Insert Words into the Trie:
- For each word, traverse the Trie, and if a character does not have a corresponding child node, create a new one. Increment the
count
for each node.
- For each word, traverse the Trie, and if a character does not have a corresponding child node, create a new one. Increment the
-
Calculate Prefix Scores:
- For each word, traverse the Trie and sum the
count
for each character to calculate the total score for the word's prefixes.
- For each word, traverse the Trie and sum the
-
Solution Function:
- Insert all words into the Trie and calculate the sum of prefix scores for each word.
- Input: A list of words.
- Output: A list of integers representing the sum of prefix scores for each word.