日野弥生:勉強しよう

LeetCode 383 - 赎金信

发表于2025年02月02日

#字符串 #计数 #哈希表

遍历左边的字符串,把右边字符串放进哈希set里进行查找。

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        for ch in ransomNote:
            # 把字符串放入集合中,每次循环过程更新set
            charSet = set(magazine)
            if ch in charSet:
                # 如果存在,则替换为空字符,即删除该字符,1为仅删除第一个
                magazine = magazine.replace(ch, "", 1)
            if ch not in charSet:
                return False
        return True
        


        

フラッシュタブ:LeetCode

题目链接:https://leetcode.cn/problems/ransom-note/

上一篇

LeetCode 1672 - 最富有客户的资产总量

下一篇

LeetCode 412 - 最富有客户的资产总量