From ced21316ec96bfa53479fc4cdd2afbd252f51edc Mon Sep 17 00:00:00 2001 From: Shubham Patrick Date: Wed, 12 Jul 2023 21:00:23 +0530 Subject: [PATCH 1/4] Create: 2306 - Naming a Company (ts,js,py) --- javascript/2306-naming-a-company.js | 40 +++++++++++++++++++++++++++++ python/2306-naming-a-company.py | 28 ++++++++++++++++++++ typescript/2306-naming-a-company.ts | 35 +++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 javascript/2306-naming-a-company.js create mode 100644 python/2306-naming-a-company.py create mode 100644 typescript/2306-naming-a-company.ts diff --git a/javascript/2306-naming-a-company.js b/javascript/2306-naming-a-company.js new file mode 100644 index 000000000..b0c839dda --- /dev/null +++ b/javascript/2306-naming-a-company.js @@ -0,0 +1,40 @@ +/** + * @param {string[]} ideas + * @return {number} + */ + +// Time complexity: O(n^2 * m) +// Space complexity: O(n * m) + +var distinctNames = function (ideas) { + const wordMap = new Map(); + let res = 0; + + ideas.forEach((word) => { + const ch = word.charAt(0); + const substr = word.slice(1); + if (!wordMap.has(ch)) { + wordMap.set(ch, new Set()); + } + wordMap.get(ch).add(substr); + }); + + for (const [ch1, set1] of wordMap.entries()) { + for (const [ch2, set2] of wordMap.entries()) { + if (ch1 === ch2) { + continue; + } + let intersect = 0; + set1.forEach((word) => { + if (set2.has(word)) { + intersect += 1; + } + }); + const distinct1 = set1.size - intersect; + const distinct2 = set2.size - intersect; + res += distinct1 * distinct2; + } + } + + return res; +}; diff --git a/python/2306-naming-a-company.py b/python/2306-naming-a-company.py new file mode 100644 index 000000000..d88aa5f01 --- /dev/null +++ b/python/2306-naming-a-company.py @@ -0,0 +1,28 @@ +# Time complexity: O(n^2 * m) +# Space complexity: O(n * m) + +from typing import List +from collections import defaultdict + + +class Solution: + def distinctNames(self, ideas: List[str]) -> int: + word_map = defaultdict(set) + res = 0 + + for word in ideas: + word_map[word[0]].add(word[1:]) + + for ch1 in word_map: + for ch2 in word_map: + if ch1 == ch2: + continue + intersect = 0 + for word in word_map[ch1]: + if word in word_map[ch2]: + intersect += 1 + distinct1 = len(word_map[ch1]) - intersect + distinct2 = len(word_map[ch2]) - intersect + res += distinct1 * distinct2 + + return res diff --git a/typescript/2306-naming-a-company.ts b/typescript/2306-naming-a-company.ts new file mode 100644 index 000000000..327392d5f --- /dev/null +++ b/typescript/2306-naming-a-company.ts @@ -0,0 +1,35 @@ +// Time complexity: O(n^2 * m) +// Space complexity: O(n * m) + +function distinctNames(ideas: string[]): number { + const wordMap: Map> = new Map(); + let res = 0; + + ideas.forEach((word) => { + const ch = word.charAt(0); + const substr = word.slice(1); + if (!wordMap.has(ch)) { + wordMap.set(ch, new Set()); + } + wordMap.get(ch)!.add(substr); + }); + + for (const [ch1, set1] of wordMap.entries()) { + for (const [ch2, set2] of wordMap.entries()) { + if (ch1 === ch2) { + continue; + } + let intersect = 0; + set1.forEach((word) => { + if (set2.has(word)) { + intersect += 1; + } + }); + const distinct1 = set1.size - intersect; + const distinct2 = set2.size - intersect; + res += distinct1 * distinct2; + } + } + + return res; +} From f9d73483523855841e0dfad6b2973d834d977867 Mon Sep 17 00:00:00 2001 From: Shubham Patrick Date: Wed, 19 Jul 2023 11:34:57 +0530 Subject: [PATCH 2/4] Update: 2306 - Naming a Company (ts,js,py) --- javascript/2306-naming-a-company.js | 35 ++++++++++++++++------------- python/2306-naming-a-company.py | 6 ++--- typescript/2306-naming-a-company.ts | 32 +++++++++++++++----------- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/javascript/2306-naming-a-company.js b/javascript/2306-naming-a-company.js index b0c839dda..bf64e43d3 100644 --- a/javascript/2306-naming-a-company.js +++ b/javascript/2306-naming-a-company.js @@ -8,33 +8,38 @@ var distinctNames = function (ideas) { const wordMap = new Map(); - let res = 0; + let count = 0; ideas.forEach((word) => { const ch = word.charAt(0); const substr = word.slice(1); - if (!wordMap.has(ch)) { - wordMap.set(ch, new Set()); - } - wordMap.get(ch).add(substr); + const set = wordMap.get(ch) ?? new Set(); + set.add(substr); + wordMap.set(ch, set); }); - for (const [ch1, set1] of wordMap.entries()) { - for (const [ch2, set2] of wordMap.entries()) { + for (const [ch1, set1] of wordMap) { + for (const [ch2, set2] of wordMap) { if (ch1 === ch2) { continue; } - let intersect = 0; - set1.forEach((word) => { - if (set2.has(word)) { - intersect += 1; - } - }); + + const intersect = calculateIntersection(set1, set2); const distinct1 = set1.size - intersect; const distinct2 = set2.size - intersect; - res += distinct1 * distinct2; + count += distinct1 * distinct2; } } - return res; + return count; }; + +function calculateIntersection(set1, set2) { + let intersect = 0; + set1.forEach((word) => { + if (set2.has(word)) { + intersect += 1; + } + }); + return intersect; +} diff --git a/python/2306-naming-a-company.py b/python/2306-naming-a-company.py index d88aa5f01..3ab0e75f6 100644 --- a/python/2306-naming-a-company.py +++ b/python/2306-naming-a-company.py @@ -8,7 +8,7 @@ class Solution: def distinctNames(self, ideas: List[str]) -> int: word_map = defaultdict(set) - res = 0 + count = 0 for word in ideas: word_map[word[0]].add(word[1:]) @@ -23,6 +23,6 @@ def distinctNames(self, ideas: List[str]) -> int: intersect += 1 distinct1 = len(word_map[ch1]) - intersect distinct2 = len(word_map[ch2]) - intersect - res += distinct1 * distinct2 + count += distinct1 * distinct2 - return res + return count diff --git a/typescript/2306-naming-a-company.ts b/typescript/2306-naming-a-company.ts index 327392d5f..2539b5470 100644 --- a/typescript/2306-naming-a-company.ts +++ b/typescript/2306-naming-a-company.ts @@ -3,15 +3,14 @@ function distinctNames(ideas: string[]): number { const wordMap: Map> = new Map(); - let res = 0; + let count = 0; ideas.forEach((word) => { const ch = word.charAt(0); const substr = word.slice(1); - if (!wordMap.has(ch)) { - wordMap.set(ch, new Set()); - } - wordMap.get(ch)!.add(substr); + const set = wordMap.get(ch) ?? new Set(); + set.add(substr); + wordMap.set(ch, set); }); for (const [ch1, set1] of wordMap.entries()) { @@ -19,17 +18,24 @@ function distinctNames(ideas: string[]): number { if (ch1 === ch2) { continue; } - let intersect = 0; - set1.forEach((word) => { - if (set2.has(word)) { - intersect += 1; - } - }); + + const intersect = calculateIntersection(set1, set2); + const distinct1 = set1.size - intersect; const distinct2 = set2.size - intersect; - res += distinct1 * distinct2; + count += distinct1 * distinct2; } } - return res; + return count; +} + +function calculateIntersection(set1: Set, set2: Set): number { + let intersect = 0; + set1.forEach((word) => { + if (set2.has(word)) { + intersect += 1; + } + }); + return intersect; } From 7239b33f757d2ad7c0920fc1449919c82b279017 Mon Sep 17 00:00:00 2001 From: Shubham Patrick Date: Fri, 21 Jul 2023 09:34:22 +0530 Subject: [PATCH 3/4] Update: 2306 - Naming a Company (ts,js,py) --- javascript/2306-naming-a-company.js | 12 ++++++------ typescript/2306-naming-a-company.ts | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/javascript/2306-naming-a-company.js b/javascript/2306-naming-a-company.js index bf64e43d3..cede631b6 100644 --- a/javascript/2306-naming-a-company.js +++ b/javascript/2306-naming-a-company.js @@ -10,13 +10,13 @@ var distinctNames = function (ideas) { const wordMap = new Map(); let count = 0; - ideas.forEach((word) => { + for (const word of ideas) { const ch = word.charAt(0); const substr = word.slice(1); const set = wordMap.get(ch) ?? new Set(); set.add(substr); wordMap.set(ch, set); - }); + } for (const [ch1, set1] of wordMap) { for (const [ch2, set2] of wordMap) { @@ -24,7 +24,7 @@ var distinctNames = function (ideas) { continue; } - const intersect = calculateIntersection(set1, set2); + const intersect = getIntersection(set1, set2); const distinct1 = set1.size - intersect; const distinct2 = set2.size - intersect; count += distinct1 * distinct2; @@ -34,12 +34,12 @@ var distinctNames = function (ideas) { return count; }; -function calculateIntersection(set1, set2) { +function getIntersection(set1, set2) { let intersect = 0; - set1.forEach((word) => { + for (const word of set1) { if (set2.has(word)) { intersect += 1; } - }); + } return intersect; } diff --git a/typescript/2306-naming-a-company.ts b/typescript/2306-naming-a-company.ts index 2539b5470..18b2a606d 100644 --- a/typescript/2306-naming-a-company.ts +++ b/typescript/2306-naming-a-company.ts @@ -5,13 +5,13 @@ function distinctNames(ideas: string[]): number { const wordMap: Map> = new Map(); let count = 0; - ideas.forEach((word) => { + for (const word of ideas) { const ch = word.charAt(0); const substr = word.slice(1); const set = wordMap.get(ch) ?? new Set(); set.add(substr); wordMap.set(ch, set); - }); + } for (const [ch1, set1] of wordMap.entries()) { for (const [ch2, set2] of wordMap.entries()) { @@ -32,10 +32,10 @@ function distinctNames(ideas: string[]): number { function calculateIntersection(set1: Set, set2: Set): number { let intersect = 0; - set1.forEach((word) => { + for (const word of set1) { if (set2.has(word)) { intersect += 1; } - }); + } return intersect; } From 1ee1dfc57f91a7e228821997f97ef3d83f7e4b93 Mon Sep 17 00:00:00 2001 From: Shubham Patrick Date: Sun, 6 Aug 2023 17:13:19 +0530 Subject: [PATCH 4/4] Update-2: 2306 - Naming a Company (ts,js,py) --- javascript/2306-naming-a-company.js | 11 ++--------- typescript/2306-naming-a-company.ts | 14 ++++---------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/javascript/2306-naming-a-company.js b/javascript/2306-naming-a-company.js index cede631b6..0ef04d826 100644 --- a/javascript/2306-naming-a-company.js +++ b/javascript/2306-naming-a-company.js @@ -34,12 +34,5 @@ var distinctNames = function (ideas) { return count; }; -function getIntersection(set1, set2) { - let intersect = 0; - for (const word of set1) { - if (set2.has(word)) { - intersect += 1; - } - } - return intersect; -} +const getIntersection = (set1, set2) => + [...set1].filter((word) => set2.has(word)).length; diff --git a/typescript/2306-naming-a-company.ts b/typescript/2306-naming-a-company.ts index 18b2a606d..e56bae59d 100644 --- a/typescript/2306-naming-a-company.ts +++ b/typescript/2306-naming-a-company.ts @@ -19,7 +19,7 @@ function distinctNames(ideas: string[]): number { continue; } - const intersect = calculateIntersection(set1, set2); + const intersect = getIntersection(set1, set2); const distinct1 = set1.size - intersect; const distinct2 = set2.size - intersect; @@ -30,12 +30,6 @@ function distinctNames(ideas: string[]): number { return count; } -function calculateIntersection(set1: Set, set2: Set): number { - let intersect = 0; - for (const word of set1) { - if (set2.has(word)) { - intersect += 1; - } - } - return intersect; -} +const getIntersection = (set1: Set, set2: Set): number => { + return [...set1].filter((word) => set2.has(word)).length; +};