JSFiddle - React, Tailwind, and code Playground
by Artem
Babel + JSX
'use strict';
function scramble(str1, str2) {
const res = [];
const tmp = str1.split('');
const keys = str1.split('').reduce((acc, next) => {
if (!acc[next]) {
acc[next] = 1;
} else {
acc[next] = acc[next] + 1;
}
return acc;
}, {});
for (let s of str2) {
if (!(s in keys)) {
return false;
} else {
const v = keys[s];
if (v > 1) {
keys[s] = keys[s] - 1;
} else {
delete keys[s];
}
//const idx = tmp.findIndex(l => l === s);
//tmp.splice(idx, 1);
//res.push(s);
}
}
return keys;
}
// console.log(scramble('codewaraaossoqqyt', 'codewars'));
console.log(scramble('rkqodlw', 'woorld'));