예제 #1
0
        public static string Solution2(string str)
        {
            Console.WriteLine(str);

            if (string.IsNullOrEmpty(str))
            {
                return(str);
            }

            var s2 = AddBoundaries(str);

            var palindromes = new Palindrome[s2.Length];

            var current = new Palindrome(0, 0);

            for (var i = 0; i < s2.Length; i++)
            {
                var min = current.Right > i
                                        ? Math.Min(current.Right - i, palindromes[current.GetMirrorCenter(i)].Length)
                                        : 0;

                palindromes[i] = new Palindrome(i, min);

                current = palindromes[i];

                while (current.Left - 1 >= 0 &&
                       current.Right + 1 < s2.Length &&
                       s2[current.Left - 1] == s2[current.Right + 1])
                {
                    current.Expand();
                }
            }

            var largest = palindromes.OrderByDescending(p => p.Length).First();

            return(RemoveBoundaries(s2.Substring(largest.Left, largest.TotalLength)));
        }