题目描述: 假设两个字符串中所含有的字符和个数都相同我们就叫这两个字符串匹配,比如:abcda和adabc, 由于出现的字符个数都是相同,只是顺序不同,所以这两个字符串是匹配的。
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
bool isMatch(const char* str1,const char * str2){
int lenOfA=strlen (str1);
int lenOfB=strlen (str2);
if(lenOfA!=lenOfB)
return false;
//建立Hash数组
int hash[26]={0};
int index=0;
for(int i=0;i<lenOfA;i++){
index=str1[i]-'A';
hash[index]++;
}
//判断字符串的匹配
for(int j=0;j<lenOfB;j++){
index=str2[j]-'A';
if(hash[index]==0)
return false;
hash[index]--;
}
return true;
}
int main(int argc, char *argv[])
{
string a="ABBA";
string b="BBAA";
if(isMatch(a.c_str(),b.c_str()))
cout<<"is match"<<endl;
else{
cout<<"Not Matched"<<endl;
}
system("pause");
return 0;
}