LeetCode 2416. 字符串的前缀分数和
原题链接
困难
作者:
我是java同学
,
2023-10-04 11:37:59
,
所有人可见
,
阅读 63
class Solution {
public:
struct Node {
Node* son[26];
int score = 0;
};
vector<int> sumPrefixScores(vector<string>& words) {
auto root = new Node();
for (auto word: words) {
auto p = root;
for (auto c: word) {
int u = c - 'a';
if (!p->son[u]) p->son[u] = new Node();
p = p->son[u];
p->score ++ ;
}
}
int n = words.size();
vector<int> res(n);
for (int i = 0; i < n; i ++ ) {
auto p = root;
for (auto c: words[i]) {
int u = c - 'a';
p = p->son[u];
res[i] += p->score;
}
}
return res;
}
};