Problem
You are given an array words
of size n
consisting of non-empty strings.
We define the score of a string word
as the number of strings words[i]
such that word
is a prefix of words[i]
.
- For example, if
words = ["a", "ab", "abc", "cab"]
, then the score of"ab"
is2
, since"ab"
is a prefix of both"ab"
and"abc"
.
Return an array answer
of size n
where answer[i]
is the sum of scores of every non-empty prefix of words[i]
.
Note that a string is considered as a prefix of itself.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Using Hashmap
To solve this problem, we can follow these steps:
- Understand the Prefix Definition: A prefix of a word is any leading segment of the word, including the word itself.
- Prefix Scoring: For each word in the list, we need to check all possible prefixes of this word and count how many words in the given list start with these prefixes.
- Sum of Scores: For each word, compute the sum of scores for all its prefixes.
Here’s a plan for the implementation:
- Generate all prefixes for each word.
- Maintain a dictionary to count occurrences of each prefix in the list.
- Use the counts to compute the score for each word according to the rules.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(m*n)
- Calculating Prefix Counts: For each word of average length
m
, generating all prefixes takesO(m)
time. Doing this forn
words results inO(n \times m)
. - Calculating Scores: For each word, adding up the counts of its prefixes also takes
O(m)
time. Thus, forn
words, this also results inO(n \times m)
. - Therefore, the overall time complexity is
O(n \times m)
.
- Calculating Prefix Counts: For each word of average length
- 🧺 Space complexity:
O(n + m^2)
- Prefix Count Map/Dictionary: In the worst case, every prefix of every word is unique. The number of such prefixes is (\sum_{i=1}^{m} n \times i = O(n \times m^2)). This is less common, and typical cases may have fewer prefixes.
- Result Array: This requires
O(n)
space. - Therefore, the overall space complexity, dominated by the prefix count map, is
O(n \times m^2)
in the worst case, and typically better depending on the number of unique prefixes.
Method 2 - Using Trie
Here are the steps:
- Initialize Trie and Result Array:
- Create the root node of the Trie.
- Initialize the result array to store scores for each word.
- Build the Trie:
- For each word in the input list:
- Reset the pointer to the root node of the Trie.
- For each character in the word:
- Calculate its index (position) in the array.
- If the corresponding child node does not exist, create it.
- Increment the visited counter for the child node.
- Move the pointer to the child node.
- For each word in the input list:
- Calculate Scores:
- For each word in the input list:
- Reset the pointer to the root node of the Trie.
- Initialize a variable to accumulate the score for the current word.
- For each character in the word:
- Calculate its index (position) in the array.
- Add the visited count of the corresponding child node to the score.
- Move the pointer to the child node.
- Store the accumulated score in the result array.
- For each word in the input list:
- Return the Result:
- Return the result array containing the scores for each word.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n * m)
- Building the Trie: For each word of average length
m
, inserting it into the Trie takesO(m)
time. Doing this forn
words results inO(n * m)
. - Calculating Scores: For each word, traversing the Trie to calculate the score also takes
O(m)
time. Thus, forn
words, this also results inO(n * m)
. - Therefore, the overall time complexity is
O(n * m)
.
- Building the Trie: For each word of average length
- 🧺 Space complexity:
O(n*m)
- In the worst case, the Trie could store every unique prefix of every word. This is generally more space-efficient than storing all prefixes in a map, but still can be bounded by
O(n * m)
, where we store each prefix node along with its count.
- In the worst case, the Trie could store every unique prefix of every word. This is generally more space-efficient than storing all prefixes in a map, but still can be bounded by