From 9864c17237002b33685da7982b72ed2d28f07161 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sun, 14 Jul 2024 19:22:32 +0800 Subject: [PATCH] 165th Commit --- .../2103. Rings and Rods/countPoints.test.ts | 9 +++++++ .../2103. Rings and Rods/countPoints.ts | 27 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/page-20/2103. Rings and Rods/countPoints.test.ts create mode 100644 src/page-20/2103. Rings and Rods/countPoints.ts diff --git a/src/page-20/2103. Rings and Rods/countPoints.test.ts b/src/page-20/2103. Rings and Rods/countPoints.test.ts new file mode 100644 index 0000000..feeda5c --- /dev/null +++ b/src/page-20/2103. Rings and Rods/countPoints.test.ts @@ -0,0 +1,9 @@ +import { countPoints } from './countPoints'; + +describe('2103. Rings and Rods', () => { + test('countPoints', () => { + expect(countPoints('B0B6G0R6R0R6G9')).toBe(1); + expect(countPoints('B0R0G0R9R0B0G0')).toBe(1); + expect(countPoints('G4')).toBe(0); + }); +}); diff --git a/src/page-20/2103. Rings and Rods/countPoints.ts b/src/page-20/2103. Rings and Rods/countPoints.ts new file mode 100644 index 0000000..ee4bcf7 --- /dev/null +++ b/src/page-20/2103. Rings and Rods/countPoints.ts @@ -0,0 +1,27 @@ +type CountPoints = (rings: string) => number; + +/** + * Accepted + */ +export const countPoints: CountPoints = (rings) => { + // Initialize a Map to store rod colors + const rodColors = new Map>(); + + // Iterate through the rings string + for (let i = 0; i < rings.length; i += 2) { + const color = rings[i]; + const rod = rings[i + 1]; + + // Add colors to the corresponding rod's set + rodColors.set(rod, (rodColors.get(rod) || new Set()).add(color)); + } + + // Count the rods with all three colors + let count = 0; + + for (const [_, colors] of rodColors) { + if (colors.size === 3) count += 1; + } + + return count; +};