This checks whether one string has all the chars in another string to make up that exact string. Counts matter so for, for example, str1 = 'abcdgbff' str2 ='aaabb'
should return false because str1
only has one 'a' and 'b'.
How can I optimize my code further?
function scramble(str1, str2) {
for (let i of str1) {
if (str2.indexOf(i) != -1) {
str2 = str2.slice(0, (str2.indexOf(i))) + str2.slice(str2.indexOf(i) + 1);
}
}
return str2.length === 0;
}
// Example:
console.log(scramble('abcdgbff', 'aaabb')); // false