Skip to content

Instantly share code, notes, and snippets.

@jricaldi
Created May 10, 2020 01:37
Show Gist options
  • Save jricaldi/534bf117bea046ca4d4538a50a884c13 to your computer and use it in GitHub Desktop.
Save jricaldi/534bf117bea046ca4d4538a50a884c13 to your computer and use it in GitHub Desktop.
Ejercicio
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