Reputation: 8846
I have a puzzle where the players need to make as many words they can, using the letters of an existing 9 letter word. Say you have "waterfall". Taking the letters and rearranging you can make wafer, flaw, left ect.
Currently, I use an expression that looks like this on my word list :
^(?:([waterfall])(?!.*?1)){1,9}$
But the problem is that this also allows letters that only appear once, to be used twice, like freeware (three Es) and tatter (three Ts).
As of now, I'm simply iterating my word list and counting letters, making sure that the suggested word does not contain a higher count per letter than my origin word. But this seems a bit tiresome, and my gut feeling tells me that it should be possible to ensure that the expression actually only uses each letter once (or twice, like the L, if it's in the origin word twice).
I have looked through the documentation, but it's all rather greek to me, and was wondering if the lovely stackers had a suggestion.
Upvotes: 2
Views: 633
Reputation: 29451
You shouldn't use a regex for it, it will be hell to maintain or generate custom regex each time. You can use this method:
public static bool checker(string big, string small)
{
Dictionary<char, int> letterCount = new Dictionary<char, int>();
foreach (char c in big)
{
if (!letterCount.ContainsKey(c))
{
letterCount[c] = 0;
}
letterCount[c]++;
}
return small.All(letter => letterCount.ContainsKey(letter) && --letterCount[letter] >= 0);
}
Upvotes: 3