MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / distinctNames

Function distinctNames

javascript/2306-naming-a-company.js:5–34  ·  view source on GitHub ↗
(ideas)

Source from the content-addressed store, hash-verified

3 * @return {number}
4 */
5var distinctNames = function (ideas) {
6 let sets = [];
7 for (let i = 0; i < 26; i++) {
8 sets[i] = new Set();
9 }
10 for (let s of ideas) {
11 sets[s.charCodeAt(0) - 97].add(s.substring(1));
12 }
13 let same = [];
14 for (let i = 0; i < 26; i++) {
15 same[i] = Array(26).fill(0);
16 }
17 for (let i = 0; i < 26; i++) {
18 for (let s of sets[i]) {
19 for (let j = i + 1; j < 26; j++) {
20 if (sets[j].has(s)) {
21 same[i][j]++;
22 }
23 }
24 }
25 }
26 let res = 0;
27 for (let i = 0; i < 26; i++) {
28 for (let j = i + 1; j < 26; j++) {
29 res +=
30 (sets[i].size - same[i][j]) * (sets[j].size - same[i][j]) * 2;
31 }
32 }
33 return res;
34};

Callers

nothing calls this directly

Calls 1

addMethod · 0.45

Tested by

no test coverage detected