0%

383. 赎金信

383. 赎金信

暴力

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool canConstruct(string ransomNote,string magazine){
for(int i =0;i<magazine.length();i++){
for(int j = 0;j<ransomNote.length();j++){
//在ransomNote中找到和magazine相同的字符
if(magazine[i]==ransomNote[j]){
ransomNote.erase(ransomNote.begin()+j);
break;
}
}
}
if(ransomNote.length()==0){
return true;
}
return false;
}

哈希

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool canConstruct(string ransomNote,string magazine){
int record[26]={0};
if(ransomNote.size()>magazine.size()){
return false;
}
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 flae;
}
}
return true;
}
-------------本文结束感谢您的阅读-------------
老板你好,讨口饭吃