-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSumOfPrefixScoresOfStrings2416.java
56 lines (47 loc) · 1.71 KB
/
SumOfPrefixScoresOfStrings2416.java
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
48
49
50
51
52
53
54
55
56
import java.util.* ;
public class SumOfPrefixScoresOfStrings2416 {
/*
* You are given an array words of size n consisting of non-empty strings.
We define the score of a string term as the number of strings words[i] such that term is a prefix of words[i].
For example, if words = ["a", "ab", "abc", "cab"], then the score of "ab" is 2, 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.
*/
class TrieNode {
TrieNode [] next = new TrieNode [26] ;
int cnt = 0;
}
class Solution {
TrieNode root = new TrieNode() ;
void insert(String word) {
TrieNode node = root ;
for (char c : word.toCharArray()) {
if (node.next[c - 'a'] == null) {
node.next[c - 'a'] = new TrieNode();
}
node = node.next[c - 'a'];
node.cnt++;
}
}
int count(String s) {
TrieNode node = root;
int ans = 0 ;
for (char c : s.toCharArray()) {
ans += node.next[c - 'a'].cnt ;
node = node.next[c - 'a'];
}
return ans;
}
public int[] sumPrefixScores(String[] words) {
int n = words.length;
for (String word : words) {
insert(word);
}
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = count(words[i]);
}
return ans;
}
}
}