Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function countChars(chars) {
- var counts = {};
- for (var char of chars) {
- if (typeof counts[char] === 'undefined') {
- counts[char] = 1;
- }
- else {
- counts[char]++;
- }
- }
- return counts;
- }
- function scramble(haystack, needle) {
- haystack = haystack.split('')
- needle = needle.split('')
- var chars = countChars(haystack);
- for(char of needle) {
- if (typeof chars[char] === 'undefined')
- {
- return false;
- }
- chars[char]--;
- if (chars[char] < 0)
- {
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement