leetcode 383. Ransom Note

本文介绍了一种解决勒索信字符串匹配问题的有效算法。通过使用unordered_map来统计字符频率,实现了快速判断是否能从杂志字符串中构造出勒索信字符串。文章提供了详细的代码实现,并讨论了可能的优化方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前记

360三面已过,后接能力测评,一切都没感觉有何异样, 然而并没有收到offer, 躺在了所谓的offer池(备胎池)中, 甚是伤感, 只能说他皮任他皮,我刷我的题~

Description

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

My solution

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        int mag[256] = {0};
        for (int i = 0; i < magazine.size(); i++) mag[magazine[i]]++;
        for (int i = 0; i < ransomNote.size(); i++) mag[ransomNote[i]]--;
        for(int i=0;i<256;i++) if(mag[i]<0) return false;
        return true;
    }
};

基本思路是没有问题的, 可优化主要有两点:

  • 内存浪费(实际26个字母用不到这么大空间)
  • 第三个for可以归入第二个for里面

Discuss

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

或者:

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        vector<int> vec(26, 0);
        for (int i = 0; i < magazine.size(); ++i)
            ++vec[magazine[i] - 'a'];
        for (int j = 0; j < ransomNote.size(); ++j)
            if (--vec[ransomNote[j] - 'a'] < 0)
                return false;
        return true;
    }
};

自己重写如下, 学会用map:

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_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[ransomNote[i]]<0) return false;
        return true;
    }
};

注意 unordered_map<char,int> mp; 可以直接++

类似的:

class Solution {
public:
    int firstUniqChar(string s) {
        unordered_map<char, int> mp;
        for (int i = 0; i < s.size(); ++i) ++mp[s[i]];
        for (int i = 0; i < s.size(); ++i) if (mp[s[i]] == 1) return i;
        return -1;
    }
};
// unordered_map
class Solution {
public:
    char findTheDifference(string s, string t) {
        unordered_map<char,int> mp;
        for(int i=0;i<s.size();++i) ++mp[s[i]];
        for(int i=0;i<t.size();++i) if(--mp[t[i]]<0) return t[i];
        return 0;
    }
};

// bit trick
class Solution {
public:
    char findTheDifference(string s, string t) {
        char res=0;
        for(int i=0;i<s.size();++i) res^=s[i];
        for(int i=0;i<t.size();++i) res^=t[i];
        return res;
    }
};

Reference

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值