C# 排列与子排列

        static StringCollection Permutations(string input)
{
if (input == null)
{
throw new Exception("input can't be null");
}
StringCollection results = new StringCollection();
char firstChar = ' ';
string remainString = "";
string temp = "";
if (input.Length<=1)
{
results.Add(input);
return results;
}

for (int i = 0; i < input.Length; i++)
{
firstChar = input[i];
remainString = input.Remove(i, 1);
StringCollection remainPermutationsString = Permutations(remainString);
for (int j = 0; j < remainPermutationsString.Count; j++)
{
temp = firstChar + remainPermutationsString[j];
results.Add(temp);
}
}
return results;
}

static StringCollection Anagram(string input)
{
StringCollection results = Permutations(input);
results.Remove(input);
return results;
}

static bool IsAnagram(string str1, string str2)
{
if (str1== null || str2 == null)
{
throw new Exception("input string can't be null");
}
StringCollection anagram = Anagram(str1);
foreach (string item in anagram)
{
if (item.Equals(str2))
{
return true;
}
}
return false;
}
原文地址:https://www.cnblogs.com/Ligeance/p/2391724.html