<p>You are given an array <code>words</code> of size <code>n</code> consisting of <strong>non-empty</strong> strings.</p>

<p>We define the <strong>score</strong> of a string <code>word</code> as the <strong>number</strong> of strings <code>words[i]</code> such that <code>word</code> is a <strong>prefix</strong> of <code>words[i]</code>.</p>

<ul>
	<li>For example, if <code>words = [&quot;a&quot;, &quot;ab&quot;, &quot;abc&quot;, &quot;cab&quot;]</code>, then the score of <code>&quot;ab&quot;</code> is <code>2</code>, since <code>&quot;ab&quot;</code> is a prefix of both <code>&quot;ab&quot;</code> and <code>&quot;abc&quot;</code>.</li>
</ul>

<p>Return <em>an array </em><code>answer</code><em> of size </em><code>n</code><em> where </em><code>answer[i]</code><em> is the <strong>sum</strong> of scores of every <strong>non-empty</strong> prefix of </em><code>words[i]</code>.</p>

<p><strong>Note</strong> that a string is considered as a prefix of itself.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;abc&quot;,&quot;ab&quot;,&quot;bc&quot;,&quot;b&quot;]
<strong>Output:</strong> [5,4,3,2]
<strong>Explanation:</strong> The answer for each string is the following:
- &quot;abc&quot; has 3 prefixes: &quot;a&quot;, &quot;ab&quot;, and &quot;abc&quot;.
- There are 2 strings with the prefix &quot;a&quot;, 2 strings with the prefix &quot;ab&quot;, and 1 string with the prefix &quot;abc&quot;.
The total is answer[0] = 2 + 2 + 1 = 5.
- &quot;ab&quot; has 2 prefixes: &quot;a&quot; and &quot;ab&quot;.
- There are 2 strings with the prefix &quot;a&quot;, and 2 strings with the prefix &quot;ab&quot;.
The total is answer[1] = 2 + 2 = 4.
- &quot;bc&quot; has 2 prefixes: &quot;b&quot; and &quot;bc&quot;.
- There are 2 strings with the prefix &quot;b&quot;, and 1 string with the prefix &quot;bc&quot;.
The total is answer[2] = 2 + 1 = 3.
- &quot;b&quot; has 1 prefix: &quot;b&quot;.
- There are 2 strings with the prefix &quot;b&quot;.
The total is answer[3] = 2.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;abcd&quot;]
<strong>Output:</strong> [4]
<strong>Explanation:</strong>
&quot;abcd&quot; has 4 prefixes: &quot;a&quot;, &quot;ab&quot;, &quot;abc&quot;, and &quot;abcd&quot;.
Each prefix has a score of one, so the total is answer[0] = 1 + 1 + 1 + 1 = 4.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words.length &lt;= 1000</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 1000</code></li>
	<li><code>words[i]</code> consists of lowercase English letters.</li>
</ul>

class Solution:
    def sumPrefixScores(self, words: List[str]) -> List[int]:
        pass


























class TrieNode:
    def __init__(self, c=''):
        self.children = {}
        self.prefixes = collections.Counter()
        self.c = c

class Solution:
    def sumPrefixScores(self, words: List[str]) -> List[int]:
        root = TrieNode()
        for w in words:
            node = root
            for i in range(len(w)):
                c = w[i]
                if c not in node.children:
                    node.children[c] = TrieNode(c=c)
                node = node.children[c]
                node.prefixes[w[:i+1]] += 1
            

        ans = []
        for w in words:
            node = root
            ans_temp = 0
            for i in range(len(w)):
                prefix = w[:i+1]
                c = w[i]
                node = node.children[c]
                ans_temp += node.prefixes[prefix]
            ans.append(ans_temp)
        return ans


class TrieNode:
    def __init__(self, c=''):
        self.children = {}
        self.prefix_counter = 0

class Solution:
    def sumPrefixScores(self, words: List[str]) -> List[int]:
        root = TrieNode()
        for w in words:
            node = root
            for i in range(len(w)):
                c = w[i]
                if c not in node.children:
                    node.children[c] = TrieNode(c=c)
                node = node.children[c]
                node.prefix_counter += 1
            

        ans = []
        for w in words:
            node = root
            ans_temp = 0
            for i in range(len(w)):
                c = w[i]
                node = node.children[c]
                ans_temp += node.prefix_counter
            ans.append(ans_temp)
        return ans


class TrieNode:
    def __init__(self):
        self.children = {}
        self.visited = 0

class Solution:
    def sumPrefixScores(self, words: List[str]) -> List[int]:
        root = TrieNode()
        for word in words:
            node = root
            for c in word:
                if c not in node.children:
                    node.children[c] = TrieNode()
                node = node.children[c]
                node.visited += 1
        

        ans = [0] * len(words)
        for idx, word in enumerate(words):
            node = root
            for c in word:
                if c in node.children:
                    node = node.children[c]
                    ans[idx] += node.visited
        return ans


















class TrieNode:
    def __init__(self):
        self.children = {}
        self.visited = 0

class Solution:
    def sumPrefixScores(self, words: List[str]) -> List[int]:
        
        def build_tree(words):
            root = TrieNode()
            for word in words:
                node = root
                for c in word:
                    if c not in node.children:
                        node.children[c] = TrieNode()
                    node = node.children[c]
                    node.visited += 1
            return root

        root = build_tree(words)
        ans = []
        for word in words:
            node = root
            cnt = 0
            for idx in range(len(word)):
                char_curr = word[idx]
                if char_curr in node.children:
                    node = node.children[char_curr]
                    cnt += node.visited
                else:
                    break
            ans.append(cnt)
        return ans





















class TrieNode:
    def __init__(self):
        self.children = {}
        self.visited = 0

class Solution:
    def sumPrefixScores(self, words: List[str]) -> List[int]:
        '''
        1. build Trie for all words
        2. add up all visited paths
        '''
        root = TrieNode()
        for word in words:
            node = root
            for c in word:
                if c not in node.children:
                    node.children[c] = TrieNode()
                node = node.children[c]
                node.visited += 1
        
        ans = []
        for word in words:
            node = root
            cnt = 0
            for c in word:
                node = node.children[c]
                cnt += node.visited
            ans.append(cnt)
        return ans























class TrieNode:
    def __init__(self):
        self.children = {}
        self.visited = 0

class Solution:
    def sumPrefixScores(self, words: List[str]) -> List[int]:
        root = TrieNode()

        for word in words:
            node = root
            for c in word:
                if c not in node.children:
                    node.children[c] = TrieNode()
                node = node.children[c]
                node.visited += 1
        
        ans = []
        for word in words:
            node = root
            cnt = 0
            for c in word:
                node = node.children[c]
                cnt += node.visited
            ans.append(cnt)
        return ans

















class TrieNode:
    def __init__(self):
        self.children = {}
        self.visited = 0

class Solution:
    def sumPrefixScores(self, words: List[str]) -> List[int]:
        n = len(words)
        ans = [0] * n

        root = TrieNode()
        for w in words:
            node = root
            for c in w:
                if c not in node.children:
                    node.children[c] = TrieNode()
                node = node.children[c]
                node.visited += 1
        
        for idx, w in enumerate(words):
            node = root
            cnt = 0
            for c in w:
                node = node.children[c]
                cnt += node.visited
            ans[idx] = cnt
        return ans
271968