leetcode 383. Ransom Note

本文介绍了一种简单的算法,用于判断一个给定的勒索信字符串是否能通过另一个包含杂志上所有字母的字符串构建而成。该算法使用了 map 数据结构来记录杂志字符串中每个字符出现的次数,并逐一检查勒索信中的字符是否能在杂志字符串中找到。

383. Ransom Note

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

简单map


class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) 
    {
        map<char, int> mp;
        for (int i = 0; i< magazine.size(); i++)
        {
            mp[magazine[i]] ++;   
        }
        for (int i = 0; i< ransomNote.size(); i++)
        {
            if (mp.find(ransomNote[i]) == mp.end()) return false;
            else if ( --mp[ransomNote[i]] == 0)    mp.erase(ransomNote[i]);
        }
        return true;
    }
};


你的代码实现了一个经典的“赎金信”问题(LeetCode 383. Ransom Note),判断是否可以用 `magazine` 中的字符构造出 `ransomNote`。整体逻辑是正确的,使用了哈希表统计字符频次,然后逐个匹配。 下面是对你代码的详细分析和优化建议: --- ### ✅ 原始代码分析 ```python class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: Dict = {} for i in range(len(magazine)): if magazine[i] in Dict: Dict[magazine[i]] += 1 else: Dict.update({magazine[i]: 1}) for i in ransomNote: if i in Dict and Dict[i] > 0: Dict[i] -= 1 else: return False return True ``` #### ✔️ 正确性: - 使用字典 `Dict` 统计 `magazine` 中每个字符出现的次数。 - 遍历 `ransomNote` 的每个字符,检查是否还有可用字符(存在且数量 > 0)。 - 如果有,则消耗一个;否则返回 `False`。 - 最终能遍历完就返回 `True`。 ✅ 功能上完全正确。 --- #### ❌ 可改进点: 1. **变量命名不规范**:`Dict` 不符合 Python 的命名规范(应小写如 `char_count`)。 2. **循环方式不够简洁**: - 第一个循环用 `for i in range(len(magazine))` 获取索引,但只用了 `magazine[i]`,不如直接遍历字符。 3. **初始化方式冗余**:可以用 `dict.get()` 或 `collections.Counter` 更简洁地完成计数。 4. **可读性和效率提升空间**:Python 提供了更优雅的方式。 --- ### ✅ 优化版本(推荐) ```python from collections import Counter class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: # 统计 magazine 中各字符的数量 char_count = Counter(magazine) # 遍历 ransomNote 中每个字符 for char in ransomNote: if char_count[char] > 0: char_count[char] -= 1 # 消耗一个字符 else: return False # 字符不足,无法构造 return True ``` --- ### 🔍 解释优化后的代码: - `Counter(magazine)`:自动统计每个字符出现的次数,等价于你手动构建的字典。 - `for char in ransomNote`:直接遍历字符,比用索引清晰得多。 - `if char_count[char] > 0`:检查该字符是否还有剩余。 - 注意:`Counter` 访问不存在的键会返回 `0`,不会报错。 - `char_count[char] -= 1`:使用掉一个字符。 - 否则返回 `False`。 这个版本更简洁、安全、易读。 --- ### 🆚 对比说明: | 特性 | 原始代码 | 优化后代码 | |------|---------|-----------| | 时间复杂度 | O(m + n) | O(m + n) | | 空间复杂度 | O(k), k为不同字符数 | O(k) | | 可读性 | 一般 | 很好 | | 命名规范 | 不规范 (`Dict`) | 规范 | | 写法简洁性 | 手动判断是否存在 | 使用 `Counter` 自动处理 | --- ### ✅ 进阶写法(一行解法,适合面试中展示思路多样性) ```python from collections import Counter class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: return not (Counter(ransomNote) - Counter(magazine)) ``` #### 解释: - `Counter(ransomNote) - Counter(magazine)` 表示 `ransomNote` 中哪些字符超出了 `magazine` 的供应。 - 如果结果为空(即布尔值为 `False`),说明可以构造。 - `not (...)` 就返回 `True`。 ⚠️ 注意:这种写法虽然短,但在面试中最好先写清楚逻辑版,再提一句“也可以用 Counter 相减一行解决”。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值