Example #1
0
        public static int CountAnagrams(IList <string> input)
        {
            var count = 0;

            foreach (var line in input)
            {
                var words     = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                var wordSet   = new HashSet <string>(words);
                var wordCount = words.Length;
                if (wordCount != wordSet.Count)
                {
                    continue;
                }
                var  letterizedWords = words.Select(word => new List <char>(word)).ToArray();
                bool valid           = true;
                foreach (var word in letterizedWords)
                {
                    var otherWords = new HashSet <string>(wordSet);
                    otherWords.ExceptWith(new[] { new string(word.ToArray()) });
                    var combinations = new Permutations <char>(word);

                    if (combinations.Any(c => otherWords.Contains(new string(c.ToArray()))))
                    {
                        valid = false;
                        break;
                    }
                }
                if (valid)
                {
                    count += 1;
                }
            }
            return(count);
        }