code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

383 Ransome Note

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
 * 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 
 */

function ransomeNote(note, mag) {
  mag.split("").forEach(char => {note = note.replace(char,"")})
  return note.length === 0;
}

test('ransome Note', () => {
  expect(ransomeNote('a', 'b')).toEqual(false)
  expect(ransomeNote('aa', 'ab')).toEqual(false)
  expect(ransomeNote('aa', 'aab')).toEqual(true)
  expect(ransomeNote('fihjjjjei', 'hjibagacbhadfaefdjaeaebgi')).toEqual(false)
});

Created 2020-04-18T19:49:15+00:00 · Edit