{"size": 1533, "ext": "js", "max_stars_repo_path": "383.ransom-note.js", "max_stars_repo_name": "Molunerfinn/LeetCode", "max_stars_repo_stars_event_min_datetime": "2019-03-25T18:10:26.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-04T03:29:06.000Z", "max_issues_repo_path": "383.ransom-note.js", "max_issues_repo_name": "Molunerfinn/LeetCode", "max_issues_repo_issues_event_min_datetime": "2019-11-07T14:38:14.000Z", "max_issues_repo_issues_event_max_datetime": "2019-11-07T15:58:26.000Z", "max_forks_repo_path": "383.ransom-note.js", "max_forks_repo_name": "Molunerfinn/LeetCode", "max_forks_repo_forks_event_min_datetime": "2020-04-21T01:57:22.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-21T01:57:22.000Z"}
{"max_stars_count": 4, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 22.2173913043, "max_line_length": 78, "alphanum_fraction": 0.6118721461}
/*
 * @lc app=leetcode id=383 lang=javascript
 *
 * [383] Ransom Note
 *
 * https://leetcode.com/problems/ransom-note/description/
 *
 * algorithms
 * Easy (49.54%)
 * Likes:    316
 * Dislikes: 112
 * Total Accepted:    111.1K
 * Total Submissions: 223.1K
 * Testcase Example:  '"a"\n"b"'
 *
 * 
 * Given an arbitrary ransom note string and another string containing letters
 * from all the magazines, write a function that will return true if the
 * ransom 
 * note can be constructed from the magazines ; otherwise, it will return
 * false. 
 * 
 * 
 * Each letter in the magazine string can only be used once in your ransom
 * note.
 * 
 * 
 * Note:
 * You may assume that both strings contain only lowercase letters.
 * 
 * 
 * 
 * canConstruct("a", "b") -> false
 * canConstruct("aa", "ab") -> false
 * canConstruct("aa", "aab") -> true
 * 
 * 
 */
/**
 * @param {string} ransomNote
 * @param {string} magazine
 * @return {boolean}
 */
var canConstruct = function(ransomNote, magazine) {
  if (magazine.length < ransomNote) {
    return false
  }
  let mLength = magazine.length
  let rLength = ransomNote.length
  let hash = {}
  for (let i = 0; i < mLength; i++) {
    if (hash[magazine[i]] === undefined) {
      hash[magazine[i]] = 1
    } else {
      hash[magazine[i]] += 1
    }
  }
  for (let i = 0; i < rLength; i++) {
    if (hash[ransomNote[i]] !== undefined && hash[ransomNote[i]] > 0) {
      hash[ransomNote[i]]--
    } else {
      return false
    }
  }
  return true
};

// console.log(canConstruct('a', 'b'))
