首页 >

字符串构造判定

 bool canConstruct(string ransomNote, string magazine) {
        string s=ransomNote, t=magazine;
        map<char, int>mp;
        map<char, int>np;
        for (int i = 0; i < s.size(); i++) mp[s[i]]++;
        for (int i = 0; i < t.size(); i++)   np[t[i]]++;
        for (auto k : mp)if (k.second > np[k.first])return false;
        return true;
    }
//挺抽象的 刚开始没看见ran和mag那个传参string 用cin>>S>>c也过了
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param ransomNote string字符串
# @param magazine string字符串
# @return bool布尔型
#
from collections import Counter

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        # write code here
        return False if len(ransomNote) > len(magazine) else not (Counter(ransomNote) - Counter(magazine))

发表于 2026-01-01 14:40:51 回复(0)
return Counter(magazine) >= Counter(ransomNote)
如果牛客支持的是python3.10而不是3.9的话,这一行应该够了
发表于 2025-12-21 16:43:34 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param ransomNote string字符串
     * @param magazine string字符串
     * @return bool布尔型
     */
    public boolean canConstruct (String ransomNote, String magazine) {
        // write code her
        HashMap<Character, Integer> hmS = new HashMap<>();
        HashMap<Character, Integer> hmT = new HashMap<>();

        for (int i = 0; i < ransomNote.length(); i++) {
            char c = ransomNote.charAt(i);
            hmS.put(c, hmS.getOrDefault(c, 0) + 1);
        }

        for (int j = 0; j < magazine.length(); j++) {
            char c = magazine.charAt(j);
            hmT.put(c, hmT.getOrDefault(c, 0) + 1);
        }

        for (Character k : hmS.keySet()) {
            if (hmT.containsKey(k) && hmS.get(k) <= hmT.get(k)) {
                continue;
            } else {
                return false;
            }
        }

        return true;

    }
}

发表于 2025-08-14 23:55:14 回复(0)
 bool canConstruct(string ransomNote, string magazine) {
        string s=ransomNote, t=magazine;
        map<char, int>mp;
        map<char, int>np;
        for (int i = 0; i < s.size(); i++) mp[s[i]]++;
        for (int i = 0; i < t.size(); i++)   np[t[i]]++;
        for (auto k : mp)if (k.second > np[k.first])return false;
        return true;
    }
//挺抽象的 刚开始没看见ran和mag那个传参string 用cin>>S>>c也过了
发表于 2026-04-16 20:57:05 回复(0)
bool canConstruct(string s, string t) {
        // write code here
        unordered_map<char, int> mp;
        for(char i:t){
            mp[i]++;
        }
        for(char i:s){
            if(mp[i]) mp[i]--;
            else return false;
        }
        return true;
    }

发表于 2026-02-07 12:54:27 回复(0)
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param ransomNote string字符串 
     * @param magazine string字符串 
     * @return bool布尔型
     */
    bool canConstruct(string ransomNote, string magazine) {
        // write code here
        vector<int> v(26, 0);
        for(char ch : magazine) v[ch - 'a']++;
        for(char ch : ransomNote){
            if(v[ch - 'a'] == 0) return false;
            v[ch - 'a']--;
        }
        return true;
    }
};

发表于 2026-01-19 17:22:49 回复(0)
#include <type_traits>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param ransomNote string字符串
* @param magazine string字符串
* @return bool布尔型
*/
bool canConstruct(string ransomNote, string magazine) {
if (ransomNote.size() > magazine.size())
return false;
else {
unordered_map <char, int> scount;
unordered_map <char, int> tcount;
for (auto i : ransomNote)
scount[i]++;
for (auto i : magazine)
tcount[i]++;
for (auto i : ransomNote) {
if (scount[i] > tcount[i])
return false;;
}
return true;
}
}
};
发表于 2025-12-17 18:07:42 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param ransomNote string字符串 
# @param magazine string字符串 
# @return bool布尔型
#
class Solution:
    def canConstruct(self , ransomNote: str, magazine: str) -> bool:
        # write code here
        if len(ransomNote) < len(magazine):
            for i in list(set(ransomNote)):
                if i not in list(set(magazine)):
                    return False
            return True
        else:
            return False

发表于 2025-12-04 11:12:52 回复(0)
测试数据有问题?
{'v', 'o', 'n', 'j', 'b', 'm', 'q', 'p', 'i', 'k', 'e', 'a', 'l', 'h', 'c', 'r', 'f', 'x', 's', 'g', 'w', 'd', 't', 'z', 'y', 'u'}
{'v', 'o', 'n', 'j', 'b', 'm', 'q', 'p', 'i', 'k', 'e', 'a', 'l', 'h', 'c', 'r', 'x', 'f', 's', 'g', 'w', 'd', 't', 'z', 'y', 'u'}
用set 都可以找到,应该是True, 预期false
发表于 2025-10-04 16:55:43 回复(0)
#include <unordered_map>
#include <unordered_set>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * @param ransomNote string字符串 
     * @param magazine string字符串 
     * @return bool布尔型
     */
    bool canConstruct(string ransomNote, string magazine) {
        int s1 = ransomNote.size();
        int count = 0;
        unordered_map<char, int> ss;
        for(auto c : magazine){             // 先将T串每个字符丢进哈希表,记录其中每种字符的出现次数
            ss[c]++;
        }
        int i = 0;
        while (s1) {
            if(ss[ransomNote[i]]!=0){    // 遍历s串,看当前字符的出现次数是否有
                count ++;                // 有就++个数,但表中的次数要减少
                ss[ransomNote[i]]--;
            }
            i++;
            s1--;
        }
        if(count == ransomNote.size()){       // 最后个数和s串一样长,就能构造出来
            return true;
        }else {
            return false;
        }
    }
};

发表于 2025-08-05 14:04:50 回复(0)