-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscramblies.js
36 lines (33 loc) · 993 Bytes
/
scramblies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.
// Notes:
// Only lower case letters will be used (a-z). No punctuation or digits will be included.
// Performance needs to be considered.
// Examples
// scramble('rkqodlw', 'world') ==> True
// scramble('cedewaraaossoqqyt', 'codewars') ==> True
// scramble('katas', 'steak') ==> False
function scramble(str1, str2) {
let str2Obj = {}, str1Obj = {};
str2.split('').map(el => {
if(!str2Obj[el]) {
str2Obj[el] = 1;
}else {
str2Obj[el]++;
}
})
str1.split('').map(el => {
if(!str1Obj[el]) {
str1Obj[el] = 1;
}else {
str1Obj[el]++;
}
})
let result = '';
if(str1.length >= str2.length) {
result = Object.keys(str2Obj).map(el => {
if(str2Obj[el] <= str1Obj[el]) return true;
return false;
})
} else return false;
return result.every(el => el === true);
}