//判断a串是否含有b串的所有字符 //前面步骤相同 for(int i=0;i<58;i++) { if(dic[i]<0) return false; if(i==57&&dic[i]>=0) return true; }
#include <iostream> #include <string> using namespace std; //判断两个字符串是否是变位词 bool anagram(string str1, string str2) { int hash[58] = {0}; for (int i = 0; i < str1.size(); i++) { int index = str1[i] - 'A'; hash[index]++; } for (int i = 0; i < str2.size(); i++) { int index = str2[i] - 'A'; hash[index]--; } for (int i = 0; i < 58; i++) { if (hash[i] != 0) return false; } return true; } int main() { cout << "输入两个字符串"; string str1, str2; cin >> str1 >> str2; int result=anagram(str1, str2); if (result == 1) cout << "true"; else cout << "false"; system("pause"); }