forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2306-naming-a-company.js
More file actions
34 lines (34 loc) · 832 Bytes
/
2306-naming-a-company.js
File metadata and controls
34 lines (34 loc) · 832 Bytes
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
/**
* @param {string[]} ideas
* @return {number}
*/
var distinctNames = function (ideas) {
let sets = [];
for (let i = 0; i < 26; i++) {
sets[i] = new Set();
}
for (let s of ideas) {
sets[s.charCodeAt(0) - 97].add(s.substring(1));
}
let same = [];
for (let i = 0; i < 26; i++) {
same[i] = Array(26).fill(0);
}
for (let i = 0; i < 26; i++) {
for (let s of sets[i]) {
for (let j = i + 1; j < 26; j++) {
if (sets[j].has(s)) {
same[i][j]++;
}
}
}
}
let res = 0;
for (let i = 0; i < 26; i++) {
for (let j = i + 1; j < 26; j++) {
res +=
(sets[i].size - same[i][j]) * (sets[j].size - same[i][j]) * 2;
}
}
return res;
};