2018-09-30 23:01:58 +01:00
---
id: 594810f028c0303b75339acc
2020-11-27 19:02:05 +01:00
title: ABC Problem
2018-09-30 23:01:58 +01:00
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302220
2021-01-13 03:31:00 +01:00
dashedName: abc-problem
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2019-02-24 19:04:29 +09:00
You are given a collection of ABC blocks (e.g., childhood alphabet blocks). There are 20 blocks with two letters on each block. A complete alphabet is guaranteed amongst all sides of the blocks. The sample collection of blocks:
2020-11-27 19:02:05 +01:00
< pre > (B O)
2019-02-24 19:04:29 +09:00
(X K)
(D Q)
(C P)
(N A)
(G T)
(R E)
(T G)
(Q D)
(F S)
(J W)
(H U)
(V I)
(A N)
(O B)
(E R)
(F S)
(L Y)
(P C)
(Z M)
< / pre >
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --instructions--
2019-03-01 17:10:50 +09:00
Implement a function that takes a string (word) and determines whether the word can be spelled with the given collection of blocks.
2020-11-27 19:02:05 +01:00
2019-03-01 17:10:50 +09:00
Some rules to keep in mind:
2020-11-27 19:02:05 +01:00
2019-03-01 17:10:50 +09:00
< ul >
< li > Once a letter on a block is used, that block cannot be used again.< / li >
< li > The function should be case-insensitive.< / li >
< / ul >
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
`canMakeWord` should be a function.
```js
assert(typeof canMakeWord === 'function');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`canMakeWord` should return a boolean.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof canMakeWord('hi') === 'boolean');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`canMakeWord("bark")` should return true.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(canMakeWord(words[0]));
```
2020-09-15 09:57:40 -07:00
2020-11-27 19:02:05 +01:00
`canMakeWord("BooK")` should return false.
```js
assert(!canMakeWord(words[1]));
```
`canMakeWord("TReAT")` should return true.
```js
assert(canMakeWord(words[2]));
```
`canMakeWord("COMMON")` should return false.
```js
assert(!canMakeWord(words[3]));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`canMakeWord("squAD")` should return true.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(canMakeWord(words[4]));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`canMakeWord("conFUSE")` should return true.
```js
assert(canMakeWord(words[5]));
```
# --seed--
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
const words = ['bark', 'BooK', 'TReAT', 'COMMON', 'squAD', 'conFUSE'];
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function canMakeWord(word) {
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
}
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-02-26 17:07:07 +09:00
function canMakeWord(word) {
2018-09-30 23:01:58 +01:00
const characters = 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM';
2018-10-02 15:02:53 +01:00
const blocks = characters.split(' ').map(pair => pair.split(''));
2018-09-30 23:01:58 +01:00
const letters = [...word.toUpperCase()];
let length = letters.length;
const copy = new Set(blocks);
letters.forEach(letter => {
for (let block of copy) {
const index = block.indexOf(letter);
if (index !== -1) {
length--;
copy.delete(block);
break;
}
}
});
return !length;
}
```