forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2416.Sum-of-Prefix-Scores-of-Strings.cpp
47 lines (44 loc) · 1.12 KB
/
2416.Sum-of-Prefix-Scores-of-Strings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
class TrieNode
{
public:
TrieNode* next[26];
int count;
TrieNode()
{
for (int i=0; i<26; i++)
next[i] = NULL;
count = 0;
}
};
public:
vector<int> sumPrefixScores(vector<string>& words) {
TrieNode* root = new TrieNode();
for (auto& word: words)
{
TrieNode* node = root;
for (char ch: word)
{
if (node->next[ch-'a']==NULL)
node->next[ch-'a'] = new TrieNode();
node = node->next[ch-'a'];
node->count += 1;
}
}
vector<int>rets;
for (auto& word: words)
{
TrieNode* node = root;
int score = 0;
for (char ch: word)
{
if (node->next[ch-'a'] == NULL)
break;
node = node->next[ch-'a'];
score += node->count;
}
rets.push_back(score);
}
return rets;
}
};