Created
November 20, 2019 05:18
-
-
Save karataev/e81a24477feb77d75509ba977a49a735 to your computer and use it in GitHub Desktop.
Find the longest words from cubes with letters
This file contains hidden or 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
const allWords = require('russian-words'); | |
function getCubesLettersHeap(cubes) { | |
return cubes.reduce((acc, cube) => { | |
const letters = cube.split(''); | |
letters.forEach(letter => { | |
acc[letter] = acc[letter]? acc[letter] + 1 : 1; | |
}); | |
return acc; | |
}, {}); | |
} | |
function getWordLettersHeap(word) { | |
return word | |
.split('') | |
.reduce((acc, letter) => { | |
acc[letter] = acc[letter] ? acc[letter] + 1 : 1; | |
return acc; | |
}, {}); | |
} | |
function findWords(cubes) { | |
const cubesLettersHeap = getCubesLettersHeap(cubes); | |
return allWords | |
.filter(word => { | |
if (word.length !== cubes.length) return false; | |
const wordLettersHeap = getWordLettersHeap(word); | |
return Object.keys(wordLettersHeap).every(key => { | |
return cubesLettersHeap[key] && cubesLettersHeap[key] > wordLettersHeap[key]; | |
}); | |
}); | |
} | |
module.exports = { | |
findWords, | |
}; | |
This file contains hidden or 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
const {findWords} = require('./helpers'); | |
const cubes = ['нмксрп', 'нмксрп', 'нгдбзж', 'ктфчло', 'нзджгб', 'пшдиам', 'бцйхлш', 'пшдиам', 'аоуэыи', 'вьиеср', 'аёиеюя', 'аоуэыи']; | |
const result = findWords(cubes); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment