1. 程式人生 > >查詢B字串中出現的字元是否在A中全部出現

查詢B字串中出現的字元是否在A中全部出現

void check(string a, string b)
{
	map<char, int>A;
	bool flag = true;
	for (int i = 0; i < a.size(); i++)//構建字串1的hash表
	{
		if (A.empty())
			A[a[i]] = 1;
		else
		{
			if (A.find(a[i]) != A.end())
				A[a[i]]++;
			else
				A[a[i]] = 1;
		}
	}
	for (int i = 0; i < b.size(); i++)
	{
		if (A.find(b[i]) != A.end())
		{
			if (A[b[i]] == 0)
			{
				cout << "false";
				flag = false;
				break;
			}
			else
			{
				A[b[i]]--;
			}
		}
		else
		{
			cout << "false";
			flag = false;
			break;
		}
	}
	if (flag == true)
		cout << "true";
}