如何做餐饮的网站,网站的付款链接怎么做,现代网站开发建设,集团网站开发费用383. 赎金信
给你两个字符串#xff1a;ransomNote 和 magazine #xff0c;判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以#xff0c;返回 true #xff1b;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
示例 1#xff1…383. 赎金信
给你两个字符串ransomNote 和 magazine 判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以返回 true 否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
示例 1 输入ransomNote “a”, magazine “b” 输出false 示例 2 输入ransomNote “aa”, magazine “ab” 输出false 示例 3 输入ransomNote “aa”, magazine “aab” 输出true 提示
1 ransomNote.length, magazine.length 1e5 ransomNote 和 magazine 由小写英文字母组成
第一种方法暴力模拟
class Solution {
public:bool canConstruct(string ransomNote, string magazine) {int record[26] {0};for(int i 0;i magazine.length();i){record[magazine[i] - a] ;}for(int j 0;j ransomNote.length();j){record[ransomNote[j] - a]--;if(record[ransomNote[j] - a] 0) {return false;}}return true;}
};但是暴力时间复杂度太高开销太大所以引进一个长度为26的记录数组先记录magazin里出现过的字母再在ransomNote中找对应
class Solution {
public:bool canConstruct(string ransomNote, string magazine) {int record[26] {0};for(int i 0;i magazine.length();i){record[magazine[i] - a] ;}for(int j 0;j ransomNote.length();j){record[ransomNote[j] - a]--;if(record[ransomNote[j] - a] 0) {return false;}}return true;}
};