Return the largest palindrome from the string Here is the question: find the largest palindrome from a string.

Ex:

ABCBAHELLOHOWRACECARAREYOUILOVEUEVOLIIAMAIDOINGGOOD


Result:

ILOVEUEVOLI


I am not sure of the efficiency of the algorithm.

static void Main(string[] args)
{
    var str = "ABCBAHELLOHOWRACECARAREYOUILOVEUEVOLIIAMAIDOINGGOOD";
    var longestPalindrome = GetLongestPalindrome(str);
    Console.WriteLine(longestPalindrome);
    Console.Read();
}

private static string GetLongestPalindrome(string input)
{
    int rightIndex = 0, leftIndex = 0;
    List&lt;string&gt; paliList = new List&lt;string&gt;();
    string currentPalindrome = string.Empty;
    string longestPalindrome = string.Empty;
    for (int currentIndex = 1; currentIndex &lt; input.Length - 1; currentIndex++)
    {
        leftIndex = currentIndex - 1;
        rightIndex = currentIndex + 1;
        while (leftIndex &gt;= 0 &amp;&amp; rightIndex &lt; input.Length)
        {
            if (input[leftIndex] != input[rightIndex])
            {
                break;
            }
            currentPalindrome = input.Substring(leftIndex, rightIndex - leftIndex + 1);
            paliList.Add(currentPalindrome);
            leftIndex--;
            rightIndex++;
        } 
    }
    var x = (from c in paliList
             select c).OrderByDescending(w =&gt; w.Length).First();
    return x; 
}
Your basic algorithm seems pretty efficient, but building a list then sorting it just to find the longest one isn't very efficient.  It would be much more efficient to just keep track of the longest palindrome:

    private static string GetLongestPalindrome(string input)
    {
        int rightIndex = 0, leftIndex = 0;
        var x = "";
        string currentPalindrome = string.Empty;
        string longestPalindrome = string.Empty;
        for(int currentIndex = 1; currentIndex &lt; input.Length - 1; currentIndex++)
        {
            leftIndex = currentIndex - 1;
            rightIndex = currentIndex + 1;
            while(leftIndex &gt;= 0 &amp;&amp; rightIndex &lt; input.Length)
            {
                if(input[leftIndex] != input[rightIndex])
                {
                    break;
                }
                currentPalindrome = input.Substring(leftIndex, rightIndex - leftIndex + 1);
                if(currentPalindrome.Length &gt; x.Length)
                    x = currentPalindrome;
                leftIndex--;
                rightIndex++;
            }
        }
        return x;
    }


In my tests this is 3 times faster.

I hate to break this to you, but there is a bug in your code, which will probably mean rewriting your algorithm.  You algorithm assumes that the palindrome will be an odd number of characters.  However, a palindrome can be an even number of characters and your code won't find it if it is.

Here's some code that will find the longest palindrome regardless:

static string LargestPalindrome(string input)
{
    string output = "";
    int minimum = 2;
    for(int i = 0; i &lt; input.Length - minimum; i++)
    {
        for(int j = i + minimum; j &lt; input.Length - minimum; j++)
        {
            string forstr = input.Substring(i, j - i);
            string revstr = new string(forstr.Reverse().ToArray());
            if(forstr == revstr &amp;&amp; forstr.Length &gt; minimum)
            {
                output = forstr;
                minimum = forstr.Length;
            }
        }
    }
    return output;
}


EDIT

The above code has a bug.  Here it is reworked:

static string LargestPalindrome(string input)
{
    int longest = 0;
    int limit = input.Length;
    for (int i = 0; i &lt; limit; i++)
    {
        for (int j = limit-1; j &gt; i; j--)
        {
            string forStr = input.Substring(i, j - i);
            string revStr = new string(forStr.Reverse().ToArray());
            if (forStr == revStr &amp;&amp; forStr.Length &gt; longest)
            {
                return forStr;
            }
        }
    }
    return "";
}