Skip to content

Commit c50df74

Browse files
committed
Hashing: Jewels and Stones
1 parent 9300f5a commit c50df74

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- [x] [Maximum Number of Balloons](./src/hashing/maximum-number-of-balloons.ts)
1818
- [x] [Contiguous Array](./src/hashing/contiguous-array.ts)
1919
- [x] [Ransom Note](./src/hashing/ransom-note.ts)
20-
- [ ] [Jewels and Stones](./src/hashing/jewels-and-stones.ts)
20+
- [x] [Jewels and Stones](./src/hashing/jewels-and-stones.ts)
2121
- [ ] [Longest Substring Without Repeating Characters](./src/hashing/longest-substring-without-repeating-characters.ts)
2222
- [ ] Linked Lists
2323
- [ ] [Middle of the Linked List](./src/linked-lists/middle-of-the-linked-list.ts)

src/hashing/jewels-and-stones.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
export function () {}
1+
export function numJewelsInStones(jewels: string, stones: string): number {
2+
const availableJews = new Set(jewels);
3+
4+
let totalJewels = 0;
5+
for (const stone of stones) {
6+
if (availableJews.has(stone)) {
7+
totalJewels++;
8+
}
9+
}
10+
11+
return totalJewels;
12+
}
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1-
import {} from '@/hashing/jewels-and-stones.js';
1+
import { numJewelsInStones } from '@/hashing/jewels-and-stones.js';
22

3-
describe.todo('Hashing: Jewels and Stones')
3+
describe('Hashing: Jewels and Stones', () => {
4+
test.each([
5+
{
6+
jewels: 'aA',
7+
stones: 'aAAbbbb',
8+
output: 3,
9+
},
10+
{
11+
jewels: 'z',
12+
stones: 'ZZ',
13+
output: 0,
14+
},
15+
{
16+
jewels: 'k',
17+
stones: 'abcD',
18+
output: 0,
19+
},
20+
])(
21+
'numJewelsInStones($jewels, $stones) === $output',
22+
({ jewels, stones, output }) => {
23+
expect(numJewelsInStones(jewels, stones)).toStrictEqual(output);
24+
}
25+
);
26+
});

0 commit comments

Comments
 (0)