Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function countChars(chars) {
  2.   var counts = {};
  3.  
  4.   for (var char of chars) {
  5.     if (typeof counts[char] === 'undefined') {
  6.       counts[char] = 1;
  7.     }
  8.     else {
  9.       counts[char]++;    
  10.     }
  11.   }
  12.  
  13.   return counts;
  14. }
  15.  
  16. function scramble(haystack, needle) {
  17.     haystack = haystack.split('')
  18.     needle = needle.split('')
  19.  
  20.     var chars = countChars(haystack);
  21.  
  22.     for(char of needle) {
  23.       if (typeof chars[char] === 'undefined')
  24.       {
  25.         return false;
  26.       }
  27.      
  28.       chars[char]--;    
  29.    
  30.       if (chars[char] < 0)
  31.       {
  32.           return false;
  33.       }
  34.     }
  35.  
  36.     return true;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement