It would be interesting to time this approach using Linq (requires .NET 3.5 or later):
private bool IsCharMatch(string stringToMatch, string stringToTest)
{
return
stringToMatch.OrderBy(s => s).SequenceEqual(stringToTest.OrderBy(s => s));
}
Edit:
Here's an "experimental" approach, using Linq 'GroupBy and 'ToDictionary methods, that addresses (hopefully) what we have learned from the OP via dialogue about the results desired:
private bool IsCharMatch2(string stringToMatch, string stringToTest)
{
if (stringToMatch.Length > stringToTest.Length) return false;
Dictionary<char, int> seqSource = stringToMatch.GroupBy(c => c)
.ToDictionary(key => key.Key, value => value.Count());
Dictionary<char, int> seqTest = stringToTest.GroupBy(c => c)
.ToDictionary(key => key.Key, value => value.Count());
int count;
foreach(var kvp in seqSource)
{
if (! seqTest.TryGetValue(kvp.Key, out count) || kvp.Value > count) return false;
}
return true;
}
Here's the result of some simple tests:
bool match1 = IsCharMatch2("hello", "olelh");
bool match2 = IsCharMatch2("A", "AA");
bool match3 = IsCharMatch2("AA", "A");
bool match4 = IsStringMadeOf("eroerfwyhpw", "whyfore");
bool match5 = IsStringMadeOf("eroerfwyhw", "whyfore");
bool match6 = IsStringMadeOf("eroerfwyhw", "wheyrxf123oryeeewz");