Skip to content

Commit 60007ef

Browse files
🥈 Scramblies
1 parent d9ad929 commit 60007ef

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎scramblies.js

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

0 commit comments

Comments
 (0)