How to optimize this validation method? Looking to optimize this method so it will run quicker, but can't seem to find anything.  WOrking in .NET 3.5.  ExcludedWords and NewCodes are both HashSet of strings.

    private bool isValid(String code)
    {
        String pattern = "[a-zA-Z]{3}";

        if (chkLessThan2Letters.Checked &amp;&amp; Regex.IsMatch(code, pattern))
        {
            return false;
        }

        foreach (string s in excludedWords)
        {
            if (code.Contains(s))
            {
                return false;
            }
        }

        if (newCodes.Contains(code))
        {
            return false;
        }

        return true;
    }
Before you optimize this, make sure that it actually needs optimization and that you're not wasting your time. Have you checked that this is a performance bottleneck in your application?

One way to optimize this is to store your excluded words in a datastructure like a Trie. This will let you see if some word in in the trie potentially faster than iterating through a list.