Advertisement
2416. Sum of Prefix Scores of Strings
HardView on LeetCode
Time: O(total chars)
Space: O(total chars)
Approach
Build Trie inserting all words; each node stores pass count; sum counts along each word's path.
2416.cs
C#
// Approach: Build Trie inserting all words; each node stores pass count; sum counts along each word's path.
// Time: O(total chars) Space: O(total chars)
public class Solution
{
private TrieNode root = new TrieNode();
public int[] SumPrefixScores(string[] words)
{
int[] ans = new int[words.Length];
foreach (var word in words)
Insert(word);
for (int i = 0; i < words.Length; ++i)
ans[i] = GetScore(words[i]);
return ans;
}
private void Insert(string word)
{
TrieNode node = root;
foreach (var c in word)
{
int i = c - 'a';
if (node.children[i] == null)
node.children[i] = new TrieNode();
node = node.children[i];
++node.count;
}
}
private int GetScore(string word)
{
TrieNode node = root;
int score = 0;
foreach (var c in word)
{
node = node.children[c - 'a'];
score += node.count;
}
return score;
}
}
public class TrieNode
{
public TrieNode[] children = new TrieNode[26];
public int count = 0;
}Advertisement
Was this solution helpful?