-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef40d69
commit 9864c17
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, Set<string>>(); | ||
|
||
// 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; | ||
}; |