示例#1
0
        public IEnumerable <string> FindPalindromes(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                throw new ArgumentException("input string cannot be null or empty");
            }

            //STEP 1. A palindrome will start and end with the same character.
            //we'll start by geting all the (unique) substrings that start and end with the same character.
            HashSet <string> substrings = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

            for (int i = 0; i < input.Length; i++)
            {
                var startAndEndChar = input[i];
                var results         = substringer.FindSubstrings(input.Substring(i), input[i]);
                if (results == null)
                {
                    break;
                }
                foreach (var result in results)
                {
                    substrings.Add(result);
                }
            }

            //STEP 2. All previously obtained substrings will be validated to see if they meet Palindrome criterion
            // If they do, they will be added to the Palindrome HashSet which will ensure uniqueness.
            foreach (var substring in substrings)
            {
                if (validator.IsValidPalindrome(substring))
                {
                    palindromeCollection.Add(substring);
                }
            }

            return(palindromeCollection);
        }