Created
May 10, 2020 01:37
-
-
Save jricaldi/534bf117bea046ca4d4538a50a884c13 to your computer and use it in GitHub Desktop.
Ejercicio
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
const { performance } = require('perf_hooks'); | |
const encontrarFrases = (target, matchings) => { | |
const hashTarget = target.toLowerCase().split('').reduce((acc, letter) => { | |
if (letter === ' ') { | |
return acc; | |
} | |
acc[letter] = (acc[letter] || 0) + 1; | |
return acc; | |
}, {}); | |
const validatedTexts = matchings.map((text, index) => { | |
const hashText = text.toLowerCase().split('').reduce((acc, letter) => { | |
if (letter === ' ') { | |
return acc; | |
} | |
acc[letter] = (acc[letter] || 0) + 1; | |
return acc; | |
}, {}); | |
let result = text; | |
for (const field in hashText) { | |
const targetLetter = hashTarget[field]; | |
if (!targetLetter || targetLetter < hashText[field]) { | |
result = undefined; | |
break; | |
} | |
} | |
return result; | |
}); | |
return validatedTexts.filter(text => typeof text !== 'undefined'); | |
} | |
const testTarget = 'Yo soy un developer'; | |
const testMatchings = [ | |
'Yo puedo', | |
'Yo puedo puedo', | |
'No coincide', | |
'pelo duro', | |
]; | |
const start = performance.now() | |
const results = encontrarFrases(testTarget, testMatchings); | |
console.log(results); | |
console.log('Time duration: ', performance.now() - start); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment