转自:http://blog.youkuaiyun.com/v_july_v/article/details/6347454
#include <iostream>
#include <string>
using namespace std;
bool Is_Match(const char *strOne,const char *strTwo)
{
int lenOfOne = strlen(strOne);
int lenOfTwo = strlen(strTwo);
// 如果长度不相等则返回false
if (lenOfOne != lenOfTwo)
return false;
// 开辟一个辅助数组并清零
int hash[26] = {0};
// 扫描字符串
for (int i = 0; i < strlen(strOne); i++)
{
// 将字符转换成对应辅助数组中的索引
int index = strOne[i] - 'A';
// 辅助数组中该索引对应元素加1,表示该字符的个数
hash[index]++;
}
// 扫描字符串
for (int j = 0; j < strlen(strTwo); j++)
{
int index = strTwo[j] - 'A';
// 如果辅助数组中该索引对应元素不为0则减1,否则返回false
if (hash[index] != 0)
hash[index]--;
else
return false;
}
return true;
}
int main()
{
string strOne = "ABBA";
string strTwo = "BBAA";
bool flag = Is_Match(strOne.c_str(), strTwo.c_str());
// 如果为true则匹配,否则不匹配
if (flag == true)
cout << "Match" << endl;
else
cout << "No Match" << endl;
return 0;
}