69 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* https://leetcode.cn/problems/ransom-note/?envType=study-plan-v2&envId=top-interview-150
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
const canConstruct = function (ransomNote, magazine) {
};
/*
思路用hash表统计magazine中的字符出现的数量之后遍历ransomNote如果ransomNote中有字符不在这个hash表中说明magazine不能
构成ransomNote如果这个字符在magazine中存在就使其计数减1如果计数为零就从哈希表中删除这个字符
*/
function f1(ransomNote, magazine) {
const map = new Map(); // 利用hash计数
for (let i = 0; i < magazine.length; i++) {
map.set(magazine[i], (map.get(magazine[i]) || 0) + 1); // 如果map中有这个字符就在原有的基础上加1没有就设置成1
}
// 遍历ransomNote中的字符如果字符在map中不存在就返回false
for (let i = 0; i < ransomNote.length; i++) {
const char = ransomNote[i];
if (map.has(char)) {
map.set(char, map.get(char) - 1);
// 如果发现当前字符计数为零从map中删除这个字符
if (map.get(char) === 0) {
map.delete(char);
}
} else {
return false;
}
}
return true;
}
/*
使用数组来代替hash表因为题目所给的测试用例只有26个小写的英文字符可以使用一个长度为26的数组数组的第一个位置代表a,最后一个位置代表
z,每个位置的值代表这个字符在magazine中出现的位置。
思考为什么使用数组会更快虽然hash查找操作时间复杂度理论上式O(1)但是hash计数到对应的链表查找数据还是需要花费时间的在数据量
大的时候可以认为是O(1),但是我们只需统计26个英文字符的数量直接使用数组数组在内存中是连续的通过偏移量查找非常快
*/
function f2(ransomNote, magazine) {
const charsCount = new Array(26).fill(0); // 统计magazine中每一个字符出现的次数
// 统计magazine中的字符
for (let i = 0; i < magazine.length; i++) {
const index = magazine.charCodeAt(i) - 97;
charsCount[index]++;
}
// 遍历ransomNote中的字符在charscount中是否存在
for (let i = 0; i < ransomNote.length; i++) {
const index = ransomNote.charCodeAt(i) - 97;
// if (charsCount[index] > 0) {
// charsCount[index]--;
// } else {
// return false;
// }
if (charsCount[index] === 0) return false;
charsCount[index]--;
}
return true;
}