We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
戳我看原题
题中提到两个字符串,一个赎金信(ransom),另外一个杂志(magazine),如果 ransom 不能由 magazine 里的字符构成,即 magzine 包含的字符是 ransom 的父集则返回 true,否则返回 false
ransom
magazine
magzine
true
false
这种存在 对照 的题目,通常都可以用 哈希表 来解决。
哈希表
我们来简单梳理下这道题的解题思路👇
0
/** * @param {string} ransomNote * @param {string} magazine * @return {boolean} */ var canConstruct = function(ransomNote, magazine) { if (ransomNote.length > magazine.length) { return false } let mgMap = new Map() for (let mg of magazine) { if (mgMap.has(mg)) { mgMap.set(mg, mgMap.get(mg) + 1) } else { mgMap.set(mg, 1) } } for (let note of ransomNote) { if (mgMap.has(note)) { const val = mgMap.get(note) if (val <= 0) { return false } mgMap.set(note, val - 1) } else { return false } } return true };
/** * @param {string} ransomNote * @param {string} magazine * @return {boolean} */ var canConstruct = function(ransomNote, magazine) { for (let mg of magazine) { if (ransomNote.includes(mg)) { ransomNote = ransomNote.replace(new RegExp(mg), '') } } return ransomNote.length === 0 };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
戳我看原题
题目剖析🧐
题中提到两个字符串,一个赎金信(
ransom
),另外一个杂志(magazine
),如果ransom
不能由magazine
里的字符构成,即magzine
包含的字符是ransom
的父集则返回true
,否则返回false
这种存在 对照 的题目,通常都可以用
哈希表
来解决。梳理逻辑💡
我们来简单梳理下这道题的解题思路👇
思路一
magazine
的哈希表
,对同个字符进行自增操作ransom
字符串,如果在哈希表
中找不到或者不够,那么返回false
,否则返回true
思路二
magzine
字符串ransom
中有字符在magazine
中找到,那么在ransom
中去除它ransom
长度为0
那么返回true
,否则返回false
示例代码🌰
解法一
解法二
The text was updated successfully, but these errors were encountered: