寻找最长可能回文的长度

时间:2015-04-18 19:06:27

标签: c# algorithm palindrome

我需要在一个单词阵列中找到回文的最大长度。我能够找到3-4个单词的长度,但是当它出现在一些棘手的单词序列中时,它会失败。我必须提取单词的第一个字符并通过检查Palindrome的最大长度。

其中一个测试用例如下。

using System;
public class CandidateCode
{    
    public static void Main()
    {
        //BBACCABB palindrom shoud return 7 
        //new string[] { "Bharti", "Bharat", "Akash", "Bhavya", "Chand", "Brijesh", "Chetak", "Arvind", "Bhavna" }));
        Console.WriteLine(FindMaxLengthPalindrome(new string[] { "Bharti", "Bharat", "Akash", "Bhavya", "Chand", "Brijesh", "Chetak", "Arvind", "Bhavna" }));
        Console.ReadLine();
    }

//function to find Maximum Length Palindrom

    public static int FindMaxLengthPalindrome(string[]  input)
    {
        string s = string.Empty;
        for(int a=0;a<input.Length;a++)
        {
            s += input[a][0].ToString();
        }    
        string maxLengthPalindrome = "";    
        if (s == null) return s.Length;    
        int len = s.Length;    
        for (int i = 0; i < len; i++)
        {
            for (int j = 0; j < len - i; j++)
            {
                bool found = true;
                for (int k = j; k < (len - j) / 2; k++)
                {
                    if (s[k] != s[len - (k - j + 1)])
                    {
                        found = false;
                        break;
                    }
                }    
                if (found)
                {
                    if (len - j > maxLengthPalindrome.Length)
                        maxLengthPalindrome = s.Substring(j, len - j);
                }    
                if (maxLengthPalindrome.Length >= (len - (i + j)))
                    break;
            }    
            if (maxLengthPalindrome.Length >= (len - i))
                break;
        }    
        return maxLengthPalindrome.Length;
    }
}

通过运行上面的代码,我得到答案6,但它应该返回7。 我在这做什么错误?

1 个答案:

答案 0 :(得分:0)

试试这个:

public static int PalindromeLengthPuzzle(string[] input1)
    {

        string firstChar = string.Empty;
        foreach (string str in input1)
        {
            firstChar = firstChar + str[0];
        }
        firstChar = getLongestPalindrome(firstChar);

        return firstChar.Length;

    }


    public static String getLongestPalindrome(String input)
    {
        int rightIndex = 0, leftIndex = 0;
        String currentPalindrome = "", longestPalindrome = "";
        for (int centerIndex = 1; centerIndex < input.Length - 1; centerIndex++)
        {
            leftIndex = centerIndex - 1; rightIndex = centerIndex + 1;
            while (leftIndex == 0 && rightIndex < input.Length)
            {
                if (input[leftIndex] != input[rightIndex])
                {
                    break;
                }
                currentPalindrome = input.Substring(leftIndex, rightIndex + 1);
                longestPalindrome = currentPalindrome.Length > longestPalindrome.Length ? currentPalindrome : longestPalindrome;
                leftIndex--; rightIndex++;
            }
        }
        return longestPalindrome;
    }