mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-11 16:00:12 -04:00
2.2 KiB
2.2 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 594810f028c0303b75339acc | ABC 问题 | 1 | 302220 | abc-problem |
--description--
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:
(B O) (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)
--instructions--
实现一个函数,该函数接受一个字符串(单词),并确定该单词是否可以用给定的块集合拼写。
要记住的一些规则:
- Once a letter on a block is used, that block cannot be used again.
- 该函数应该不区分大小写。
--hints--
canMakeWord 应该是一个函数。
assert(typeof canMakeWord === 'function');
canMakeWord 应该返回一个布尔值。
assert(typeof canMakeWord('hi') === 'boolean');
canMakeWord("bark") 应该返回 true。
assert(canMakeWord(words[0]));
canMakeWord("BooK") 应该返回 false。
assert(!canMakeWord(words[1]));
canMakeWord("TReAT") 应该返回 true。
assert(canMakeWord(words[2]));
canMakeWord("COMMON") should return false.
assert(!canMakeWord(words[3]));
canMakeWord("squAD") 应该返回 true。
assert(canMakeWord(words[4]));
canMakeWord("conFUSE") 应该返回 true。
assert(canMakeWord(words[5]));
--seed--
--after-user-code--
const words = ['bark', 'BooK', 'TReAT', 'COMMON', 'squAD', 'conFUSE'];
--seed-contents--
function canMakeWord(word) {
}
--solutions--
function canMakeWord(word) {
const characters = 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM';
const blocks = characters.split(' ').map(pair => pair.split(''));
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;
}