赎金信

两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。

  • 输入:ransomNote = “a”, magazine = “b”

  • 输出:false

  • 输入:ransomNote = “aa”, magazine = “ab”

  • 输出:false

  • 输入:ransomNote = “aa”, magazine = “aab”

  • 输出:true

1
2
3
4
5
6
7
8
var canConstruct = function(ransomNote, magazine) {
for (let mg of magazine) {
if (ransomNote.includes(mg)) {
ransomNote = ransomNote.replace(new RegExp(mg), '')
}
}
return ransomNote.length === 0
};