1. 假设两个字符串中所含有的字符和个数都相同我们就叫这两个字符串匹配,比如:abcda和adabc,由于出现的字符个数都是相同,只是顺序不同,所以这两个字符串是匹配的。要求高效!
假定字符串中都是ASCII字符。如下用一个数组来计数,前者加,后者减,全部为0则匹配。
static bool IsMatch(string s1, string s2)
{
if (s1 == null && s2 == null) return true;
if (s1 == null || s2 == null) return false;
if (s1.Length != s2.Length) return false;
int[] check = new int[128];
foreach (char c in s1)
{
check[(int)c]++;
}
foreach (char c in s2)
{
check[(int)c]--;
}
foreach (int i in check)
{
if (i != 0) return false;
}
return true;
}