编写一个函数,该函数返回给定字符串中最长的回文


101

例如字符串“ abaccddccefe”中的“ ccddcc”

我想到了一个解决方案,但它的运行时间为O(n ^ 2)

算法1:

步骤:这是一种蛮力方法


  1. 对于i = 1至i小于array.length的2个for循环,
    对于j = i + 1至j小于 array.length的-1
  2. 这样,您可以从数组中获取所有可能组合的子字符串
  3. 具有回文功能,可检查字符串是否为回文
  4. 因此,对于每个子串(i,j)都调用此函数(如果它是回文)将其存储在字符串变量中
  5. 如果找到下一个回文子字符串,并且该子字符串大于当前子字符串,则将其替换为当前子字符串。
  6. 最后,您的字符串变量将得到答案

问题:1.该算法运行时间为O(n ^ 2)。

算法2:

  1. 反转字符串并将其存储在不同的数组中
  2. 现在找到两个数组之间最大的匹配子字符串
  3. 但这也需要O(n ^ 2)时间

你们能想到一种运行时间更好的算法吗?如果可能的话O(n)时间


42
我认为第一个是O(n^2)获取子字符串* O(n)来检查它们是否是回文,总共是O(n^3)
Skylar Saveland

如果我知道我正在使用回文式并将字符串保存为两半,然后如果使用Java,我会用O(1)检查该函数怎么办?
viki.omega13年

10
secong算法是正确的吗?字符串呢:“ abcdecba”。最大的匹配子字符串是(“ abcdecba”与“ abcedcba”):“ abc”或“ cba”。但是,两者都不是回文。
Yarneo

@Learner,只是很好奇,在您的步骤上方,您引用的是哪个数组?通过数组您是指字符串吗?string.length?
Zolt

1
对于那些用O(n ^ 2)搜索的答案-geeksforgeeks.org/longest-palindrome-substring-set-1
Shirish Herwade

Answers:


76

您可以使用最长的回文Manacher算法O(n)时间!它的实现可以在这里这里找到。

对于输入,String s = "HYTBCABADEFGHABCDEDCBAGHTFYW1234567887654321ZWETYGDE"它将找到正确的输出1234567887654321


3
我不明白这是线性的。我看到while嵌入for的边框似乎与外循环类似。
v.oddou '16


9

Algo 2可能不适用于所有琴弦。这是这样的字符串“ ABCDEFCBA”的示例。

并不是说字符串的子字符串是“ ABC”和“ CBA”。如果反转原始字符串,则为“ ABCFEDCBA”。最长匹配的子字符串是“ ABC”,不是回文。

您可能需要另外检查这个最长的匹配子字符串是否实际上是运行时间为O(n ^ 3)的回文。


2
重要的是要注意Algo 2应该适用于“最长匹配子序列回文”,这是一个常见的算法问题,子序列字符也可以在字符串中分开。例如,上述两个字符串之间最长的匹配子序列(包括字符分隔符)是“ ABCFCBA”,这也是一个回文:)这里是描述LCS问题的链接: ics.uci.edu/~eppstein/161/960229.html
杰克·德鲁

5

据我了解的问题,我们可以在中心索引附近找到回文,并在中心的左右两侧进行搜索。鉴于此,并且知道输入的角落没有回文,我们可以将边界设置为1和length-1。在注意String的最小和最大边界时,我们验证对称索引位置(右和左)的字符对于每个中心位置是否都相同,直到达到最大上限中心。

外循环为O(n)(最多n-2次迭代),内循环为O(n)(最大(n / 2)-1次迭代)

这是我使用其他用户提供的示例的Java实现。

class LongestPalindrome {

    /**
     * @param input is a String input
     * @return The longest palindrome found in the given input.
     */
    public static String getLongestPalindrome(final 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.charAt(leftIndex) != input.charAt(rightIndex)) {
                    break;
                }
                currentPalindrome = input.substring(leftIndex, rightIndex + 1);
                longestPalindrome = currentPalindrome.length() > longestPalindrome.length() ? currentPalindrome : longestPalindrome;
                leftIndex--;  rightIndex++;
            }
        }
        return longestPalindrome;
    }

    public static void main(String ... args) {
        String str = "HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE";
        String longestPali = getLongestPalindrome(str);
        System.out.println("String: " + str);
        System.out.println("Longest Palindrome: " + longestPali);
    }
}

其输出如下:

marcello:datastructures marcello$ javac LongestPalindrome
marcello:datastructures marcello$ java LongestPalindrome
String: HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE
Longest Palindrome: 12345678987654321

6
如果我给“ HYTBCABADEFGHABCDEDCBAGHTFYW1234567887654321ZWETYGDE”,则不起作用,但应为1234567887654321
Elbek 2012年

1
@j_random_hacker不,那是二次解法之一。它覆盖在这里expandAroundCenter
v.oddou '16

@ v.oddou:绝对正确,鉴于只有2个嵌套循环,我不知道如何得出O(n ^ 3)!我将删除该错误的评论...但是我也注意到该解决方案存在问题,我将在另一个评论中提出该问题,以便作者希望注意到。
j_random_hacker '16

我先前对O(n ^ 3)时间复杂度的主张是错误的(感谢@ v.oddou指出了这一点!),但是还有另一个问题:此代码未考虑偶数回文。 这可以通过增加第二个,非常相似的外环是固定的(也为O(n ^ 2),所以它不会影响为O(n ^ 2)时间复杂度)而膨胀围绕每个n-1个位置的回文之间各对字符。+2,如果您修复:)
j_random_hacker '16

2

使用正则表达式和红宝石,您可以像这样扫描短回文:

PROMPT> irb
>> s = "longtextwithranynarpalindrome"
=> "longtextwithranynarpalindrome"
>> s =~ /((\w)(\w)(\w)(\w)(\w)\6\5\4\3\2)/; p $1
nil
=> nil
>> s =~ /((\w)(\w)(\w)(\w)\w\5\4\3\2)/; p $1
nil
=> nil
>> s =~ /((\w)(\w)(\w)(\w)\5\4\3\2)/; p $1
nil
=> nil
>> s =~ /((\w)(\w)(\w)\w\4\3\2)/; p $1
"ranynar"
=> nil

2

我出于好奇,简单和不言自明的HTH编写了以下Java程序。谢谢。

/**
 *
 * @author sanhn
 */
public class CheckPalindrome {

    private static String max_string = "";

    public static void checkSubString(String s){
        System.out.println("Got string is "+s);
        for(int i=1;i<=s.length();i++){
            StringBuilder s1 = new StringBuilder(s.substring(0,i));
            StringBuilder s2 = new StringBuilder(s.substring(0,i));
            s2.reverse();
            if(s1.toString().equals(s2.toString())){
                if(max_string.length()<=s1.length()){
                    max_string = s1.toString();
                    System.out.println("tmp max is "+max_string);
                }

            }
        }
    }

    public static void main(String[] args){
        String s="HYTBCABADEFGHABCDEDCBAGHTFYW1234567887654321ZWETYGDE";

        for(int i=0; i<s.length(); i++)
            checkSubString(s.substring(i, s.length()));

        System.out.println("Max string is "+max_string);
    }
}

1

最近有人问我这个问题。这是我(最终)提出的解决方案。我用JavaScript编写了代码,因为它在该语言中非常简单。

基本概念是,您在弦上行走以寻找可能的最小多字符回文(两个或三个字符)。一旦有了它,扩大双方的边界,直到不再是回文。如果该长度大于当前最长的长度,请存储并继续移动。

// This does the expanding bit.
function getsize(s, start, end) {
    var count = 0, i, j;
    for (i = start, j = end; i >= 0 && j < s.length; i--, j++) {
        if (s[i] !== s[j]) {
            return count;
        }
        count = j - i + 1; // keeps track of how big the palindrome is
    }
    return count;
}

function getBiggestPalindrome(s) {
    // test for simple cases
    if (s === null || s === '') { return 0; }
    if (s.length === 1) { return 1; }
    var longest = 1;
    for (var i = 0; i < s.length - 1; i++) {
        var c = s[i]; // the current letter
        var l; // length of the palindrome
        if (s[i] === s[i+1]) { // this is a 2 letter palindrome
            l = getsize(s, i, i+1);
        }
        if (i+2 < s.length && s[i] === s[i+2]) { // 3 letter palindrome
            l = getsize(s, i+1, i+1);
        }
        if (l > longest) { longest = l; }
    }
    return longest;
}

可以肯定地将其清理和优化一些,但是除了最坏的情况(字符串相同的字母)以外,它在所有情况下都应该具有相当好的性能。


1
我本来以为OP的算法#1是O(n ^ 2)时间,但实际上是O(n ^ 3),所以即使您的算法并没有完全达到可实现的O(n)界限,仍然是一个进步。
j_random_hacker 2012年

1
您称其为“直截了当”,但它充满了i j l s if状态维护。多返回点,边缘情况……
v.oddou

1

嗨,这是我的代码,用于查找字符串中最长的回文。请参考以下链接以了解算法http://stevekrenzel.com/articles/longest-palnidrome

使用的测试数据是HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE

 //Function GetPalindromeString

public static string GetPalindromeString(string theInputString)
 { 

        int j = 0;
        int k = 0;
        string aPalindrome = string.Empty;
        string aLongestPalindrome = string.Empty ;          
        for (int i = 1; i < theInputString.Length; i++)
        {
            k = i + 1;
            j = i - 1;
            while (j >= 0 && k < theInputString.Length)
            {
                if (theInputString[j] != theInputString[k])
                {
                    break;
                }
                else
                {
                    j--;
                    k++;
                }
                aPalindrome = theInputString.Substring(j + 1, k - j - 1);
                if (aPalindrome.Length > aLongestPalindrome.Length)
                {
                    aLongestPalindrome = aPalindrome;
                }
            }
        }
        return aLongestPalindrome;     
  }

我不确定这是否适用于长度相等的回文...可以确认吗?
st0le 2011年

此方法甚至适用于回文症,您可以运行该程序,让我知道是否不适合您。有关算法的理解,请参考以下链接stevekrenzel.com/articles/longest-palnidrome
Mohit Bhandari

@ st0le:这种逻辑即使在回文中也不起作用,但即使在回文中也可以调整。请为我的早期命令感到遗憾。我得到了逻辑,我将在几天后更新它。
Mohit Bhandari'3

直到今天再也没有看过您以前的评论。
st0le 2011年

2
我本来以为OP的算法#1是O(n ^ 2)时间,但实际上是O(n ^ 3),所以即使您的算法并没有完全达到可实现的O(n)界限,仍然是一个进步。
j_random_hacker 2012年

1

请参阅有关此主题的Wikipedia文章。以下文章中的示例Manacher的线性O(n)解决方案的Algorithm Java实现:

导入java.util.Arrays; 公共类ManachersAlgorithm {公共静态字符串findLongestPalindrome(String s){如果(s == null || s.length()== 0)返回“”;

char[] s2 = addBoundaries(s.toCharArray());
int[] p = new int[s2.length]; 
int c = 0, r = 0; // Here the first element in s2 has been processed.
int m = 0, n = 0; // The walking indices to compare if two elements are the same
for (int i = 1; i<s2.length; i++) {
  if (i>r) {
    p[i] = 0; m = i-1; n = i+1;
  } else {
    int i2 = c*2-i;
    if (p[i2]<(r-i)) {
      p[i] = p[i2];
      m = -1; // This signals bypassing the while loop below. 
    } else {
      p[i] = r-i;
      n = r+1; m = i*2-n;
    }
  }
  while (m>=0 && n<s2.length && s2[m]==s2[n]) {
    p[i]++; m--; n++;
  }
  if ((i+p[i])>r) {
    c = i; r = i+p[i];
  }
}
int len = 0; c = 0;
for (int i = 1; i<s2.length; i++) {
  if (len<p[i]) {
    len = p[i]; c = i;
  }
}
char[] ss = Arrays.copyOfRange(s2, c-len, c+len+1);
return String.valueOf(removeBoundaries(ss));   }
private static char[] addBoundaries(char[] cs) {
if (cs==null || cs.length==0)
  return "||".toCharArray();

char[] cs2 = new char[cs.length*2+1];
for (int i = 0; i<(cs2.length-1); i = i+2) {
  cs2[i] = '|';
  cs2[i+1] = cs[i/2];
}
cs2[cs2.length-1] = '|';
return cs2;   }
private static char[] removeBoundaries(char[] cs) {
if (cs==null || cs.length<3)
  return "".toCharArray();

char[] cs2 = new char[(cs.length-1)/2];
for (int i = 0; i<cs2.length; i++) {
  cs2[i] = cs[i*2+1];
}
return cs2;   }     }

1

Regexp避免暴力破解的有效解决方案

从整个字符串长度开始,一直向下到2个字符,一旦进行匹配就存在

对于"abaccddccefe"regexp测试,返回前有7个匹配项ccddcc

(。)(。)(。)(。)(。)(。)(\ 6)(\ 5)(\ 4)(\ 3)(\ 2)(\ 1)
(。)(。)(。 )(。)(。)(。)(\ 5)(\ 4)(\ 3)(\ 2)(\ 1)
(。)(。)(。)(。)(。)(\ 5)( \ 4)(\ 3)(\ 2)(\ 1)
(。)(。)(。)(。)(。)(\ 4)(\ 3)(\ 2)(\ 1)
(。)( 。)(。)(。)(\ 4)(\ 3)(\ 2)(\ 1)
(。)(。)(。)(。)(\ 3)(\ 2)(\ 1)
(。 )(。)(。)(\ 3)(\ 2)(\ 1)

Dim strTest
wscript.echo Palindrome("abaccddccefe")

Sub Test()
Dim strTest
MsgBox Palindrome("abaccddccefe")
End Sub

功能

Function Palindrome(strIn)

Set objRegex = CreateObject("vbscript.regexp")

For lngCnt1 = Len(strIn) To 2 Step -1
    lngCnt = lngCnt1 \ 2
    strPal = vbNullString

    For lngCnt2 = lngCnt To 1 Step -1
        strPal = strPal & "(\" & lngCnt2 & ")"
    Next

    If lngCnt1 Mod 2 = 1 Then strPal = "(.)" & strPal

    With objRegex
        .Pattern = Replace(Space(lngCnt), Chr(32), "(.)") & strPal
        If .Test(strIn) Then
            Palindrome = .Execute(strIn)(0)
            Exit For
        End If
    End With
Next

End Function

@DickKusleika您能否使用上面的修订代码在dailydoseofexcel.com/archives/2016/01/14/…上更新我的评论。Thx
brettdj

1
public static void main(String[] args) {
         System.out.println(longestPalindromeString("9912333321456")); 
}

    static public String intermediatePalindrome(String s, int left, int right) {
        if (left > right) return null;
        while (left >= 0 && right < s.length()
                && s.charAt(left) == s.charAt(right)) {
            left--;
            right++;
        }
        return s.substring(left + 1, right);
    }


    public static String longestPalindromeString(String s) {
        if (s == null) return null;
        String longest = s.substring(0, 1);
        for (int i = 0; i < s.length() - 1; i++) {
            //odd cases like 121
            String palindrome = intermediatePalindrome(s, i, i);
            if (palindrome.length() > longest.length()) {
                longest = palindrome;
            }
            //even cases like 1221
            palindrome = intermediatePalindrome(s, i, i + 1);
            if (palindrome.length() > longest.length()) {
                longest = palindrome;
            }
        }
        return longest;
    }

0

尝试字符串-“ HYTBCABADEFGHABCDEDCBAGHTFYW123456789987654321ZWETYGDE”;它应该适用于偶数和奇数朋友。非常感谢Mohit!

使用命名空间std;

string largestPal(string input_str)
{
  string isPal = "";
  string largest = "";
  int j, k;
  for(int i = 0; i < input_str.length() - 1; ++i)
    {
      k = i + 1;
      j = i - 1;

      // starting a new interation                                                      
      // check to see if even pal                                                       
      if(j >= 0 && k < input_str.length()) {
        if(input_str[i] == input_str[j])
          j--;
        else if(input_str[i] == input_str[j]) {
          k++;
        }
      }
      while(j >= 0 && k < input_str.length())
        {
          if(input_str[j] != input_str[k])
            break;
          else
            {
              j--;
              k++;
            }
          isPal = input_str.substr(j + 1, k - j - 1);
            if(isPal.length() > largest.length()) {
              largest = isPal;
            }
        }
    }
  return largest;
}

2
几乎可以在O(n ^ 2)时间内完成。为什么要构建isPal-O(n)操作-仅测量其长度!此外,它在处理回文方面也有很多错误。关于偶数回文​​的错误:else if(input_str[i] == input_str[j])永远无法成功,因为同一条测试必须在上一条if语句中失败;它的马车无论如何,因为你不能告诉只要看看2个字符间隔2个位置除了你是否正在寻找一个更回文或奇数一个(考虑AAAAAAA)。
j_random_hacker 2012年

0

以下代码为偶数长度和奇数长度的字符串计算Palidrom。

不是最佳解决方案,但在两种情况下都适用

HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE HYTBCABADEFGHABCDEDCBAGHTFYW1234567887654321ZWETYGDE

private static String getLongestPalindrome(String string) {
    String odd = getLongestPalindromeOdd(string);
    String even = getLongestPalindromeEven(string);
    return (odd.length() > even.length() ? odd : even);
}

public static String getLongestPalindromeOdd(final String input) {
    int rightIndex = 0, leftIndex = 0;
    String currentPalindrome = "", longestPalindrome = "";
    for (int centerIndex = 1; centerIndex < input.length() - 1; centerIndex++) {
        leftIndex = centerIndex;
        rightIndex = centerIndex + 1;
        while (leftIndex >= 0 && rightIndex < input.length()) {
            if (input.charAt(leftIndex) != input.charAt(rightIndex)) {
                break;
            }
            currentPalindrome = input.substring(leftIndex, rightIndex + 1);
            longestPalindrome = currentPalindrome.length() > longestPalindrome
                    .length() ? currentPalindrome : longestPalindrome;
            leftIndex--;
            rightIndex++;
        }
    }
    return longestPalindrome;
}

public static String getLongestPalindromeEven(final 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.charAt(leftIndex) != input.charAt(rightIndex)) {
                break;
            }
            currentPalindrome = input.substring(leftIndex, rightIndex + 1);
            longestPalindrome = currentPalindrome.length() > longestPalindrome
                    .length() ? currentPalindrome : longestPalindrome;
            leftIndex--;
            rightIndex++;
        }
    }
    return longestPalindrome;
}

0
  1. 修改字符串以使用分隔符分隔每个字符[这是为了合并奇数和偶数回文]
  2. 在每个角色周围找到回文,将其视为中心

我们可以用这个找到所有长度的回文。

样品:

字= abcdcbc

modifiedString = a#b#c#d#c#b#c

palinCount = 1010105010301

最长回文的长度= 5;

最长回文= bcdcb

公共类MyLongestPalindrome {

static String word;
static int wordlength;
static int highestcount = 0;
static int newlength;
static char[] modifiedString; // stores modified string
static int[] palinCount; // stores palindrome length at each position
static char pound = '#';

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    System.out.println("Enter String : ");
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader bfr = new BufferedReader(isr);
    word = bfr.readLine();
    wordlength = word.length();
    newlength = (wordlength * 2) - 1;
    convert();
    findpalindrome();
    display();
}

// Inserting # in string
public static void convert() {

    modifiedString = new char[newlength];
    int j = 0;
    int i;
    for (i = 0; i < wordlength - 1; i++) {
        modifiedString[j++] = word.charAt(i);
        modifiedString[j++] = pound;
    }
    modifiedString[j] = word.charAt(i);
}

// display all palindromes of highest length
public static void display() {
    String palindrome;
    String s = new String(modifiedString);
    System.out.println("Length of longest palindrome = " + highestcount);
    for (int i = 0; i < newlength; i++) {
        if (palinCount[i] == highestcount) {
            palindrome = s.substring(i - (highestcount - 1), i
                    + (highestcount));
            i = i + (highestcount - 1);
            palindrome = palindrome.replace("#", "");
            System.out.println(palindrome);
        }
    }
}

// populate palinCount with length of palindrome string at each position
public static void findpalindrome() {
    int left, right, count;
    palinCount = new int[newlength];
    palinCount[0] = 1;
    palinCount[newlength - 1] = 1;
    for (int i = 1; i < newlength - 1; i++) {
        count = 0;
        left = i - 1;
        right = i + 1;
        ;
        if (modifiedString[i] != pound)
            count++;
        while (left >= 0 && right < newlength) {
            if (modifiedString[left] == modifiedString[right]) {
                if (modifiedString[left] != pound)
                    count = count + 2;
                left--;
                right++;
            } else
                break;
        }

        palinCount[i] = count;
        highestcount = count > highestcount ? count : highestcount;

    }

}

}


0

这将从给定的字符串中返回最长的回文字符串

-(BOOL)isPalindromString:(NSString *)strInput
{
    if(strInput.length<=1){
        return NO;
    }
    int halfLenth = (int)strInput.length/2;

    BOOL isPalindrom = YES;
    for(NSInteger i=0; i<halfLenth; i++){

        char a = [strInput characterAtIndex:i];
        char b = [strInput characterAtIndex:(strInput.length-1)-i];

        if(a != b){
            isPalindrom = NO;
            break;
        }
    }
    NSLog(@"-%@- IS Plaindrom %@",strInput,(isPalindrom ? @"YES" : @"NO"));
    return isPalindrom;
}


-(NSString *)longestPalindrom:(NSString *)strInput
{
    if(strInput.length<=1){
        return @"";
    }

    NSString *strMaxPalindrom = @"";

    for(int i = 0; i<strInput.length ; i++){

        for(int j = i; j<strInput.length ; j++){

            NSString *strSub = [strInput substringWithRange:NSMakeRange(i, strInput.length-j)];

            if([self isPalindromString:strSub]){

                if(strSub.length>strMaxPalindrom.length){

                    strMaxPalindrom = strSub;
                }
            }
        }
    }
    NSLog(@"-Max - %@",strMaxPalindrom);
    return strMaxPalindrom;
}

-(void)test
{
    [self longestPalindrom:@"abcccbadeed"];
}

==输出===

输入:abcccde输出:ccc

输入:abcccbd输出:bcccb

输入:abedccde输出:edccde

输入:abcccdeed输出:契约

输入:abcccbadeed输出:abcccba


0

这是JavaScript的实现:

var longestPalindromeLength = 0;
var longestPalindrome = ''

function isThisAPalidrome(word){
  var reverse = word.split('').reverse().join('')
  return word == reverse
}

function findTheLongest(word){ // takes a word of your choice
  for(var i = 0; i < word.length; i++){ // iterates over each character
    var wordMinusOneFromBeginning = word.substr(i, word.length) // for each letter, create the word minus the first char
    for(var j = wordMinusOneFromBeginning.length; j > 0; j--){ // for the length of the word minus the first char
      var wordMinusOneFromEnding = wordMinusOneFromBeginning.substr(0, j) // create a word minus the end character
      if(wordMinusOneFromEnding <= 0) // make sure the value is more that 0,
      continue // if more than zero, proced to next if statement
      if(isThisAPalidrome(wordMinusOneFromEnding)){ // check if the word minus the first character, minus the last character = a plaindorme
        if(wordMinusOneFromEnding.length > longestPalindromeLength){ // if it is
          longestPalindromeLength = wordMinusOneFromEnding.length; // save its length
          longestPalindrome = wordMinusOneFromEnding // and save the string itself
        } // exit the statement that updates the longest palidrome
      } // exit the stament that checks for a palidrome
    } // exit the loop that goes backwards and takes a letter off the ending
  } // exit the loop that goes forward and takes off the beginning letter
  return console.log('heres the longest string: ' + longestPalindrome
  + ' its ' + longestPalindromeLength + ' charachters in length'); // return the longest palidrome! :)
}
findTheLongest('bananas');


不知道为什么这被否决了-就像一个魅力。通过对CA技术的采访让我很好。
亚历克斯·贝内特

0

对于线性解,可以使用Manacher算法。还有另一种称为Gusfield算法的算法,下面是Java中的代码:

public class Solution {  
    char[] temp;   
    public int match(int a, int b,int len){   
        int i = 0;   
        while (a-i>=0 && b+i<len && temp[a-i] == temp[b+i]) i++;   
        return i;   
    }  

    public String longestPalindrome(String s) {  

        //This makes use of the assumption that the string has not more than 1000 characters.  
        temp=new char[1001*2];  
        int[] z=new int[1001 * 2];  
        int L=0, R=0;  
        int len=s.length();  

        for(int i=0;i<len*2+1;i++){  
            temp[i]='.';  
        }  

        for(int i=0;i<len;++i){  
            temp[i*2+1] = s.charAt(i);  
        }  

        z[0]=1;  
        len=len*2+1;  

        for(int i=0;i<len;i++){  
            int ii = L - (i - L);     
            int n = R + 1 - i;  
            if (i > R)  
            {  
                z[i] = match(i, i,len);  
                L = i;  
                R = i + z[i] - 1;  
            }  
            else if (z[ii] == n)  
            {  
                z[i] = n + match(i-n, i+n,len);  
                L = i;  
                R = i + z[i] - 1;  
            }  
            else  
            {  
                z[i] = (z[ii]<= n)? z[ii]:n;  
            }   
        }  

        int n = 0, p = 0;  
        for (int i=0; i<len; ++i)  
            if (z[i] > n)  
                n = z[p = i];  

        StringBuilder result=new StringBuilder();  
        for (int i=p-z[p]+1; i<=p+z[p]-1; ++i)  
            if(temp[i]!='.')  
                result.append(String.valueOf(temp[i]));  

        return result.toString();  
    }  
}  

您可以在我自己的博客中找到其他解决方案的更多信息,例如最佳O(n ^ 2)解决方案或Manacher算法。


0

在这里我写了一个逻辑试试看:)

public class palindromeClass{

public  static String longestPalindromeString(String in) {
        char[] input = in.toCharArray();
        int longestPalindromeStart = 0;
        int longestPalindromeEnd = 0;

        for (int mid = 0; mid < input.length; mid++) {
            // for odd palindrome case like 14341, 3 will be the mid
            int left = mid-1;
            int right = mid+1;
            // we need to move in the left and right side by 1 place till they reach the end
            while (left >= 0 && right < input.length) {
                // below check to find out if its a palindrome
                if (input[left] == input[right]) {
                    // update global indexes only if this is the longest one till now
                    if (right - left > longestPalindromeEnd
                            - longestPalindromeStart) {
                        longestPalindromeStart = left;
                        longestPalindromeEnd = right;
                    }
                }
                else
                    break;
                left--;
                right++;
            }
            // for even palindrome, we need to have similar logic with mid size 2
            // for that we will start right from one extra place
            left = mid;
            right = mid + 1;// for example 12333321 when we choose 33 as mid
            while (left >= 0 && right < input.length)
            {
                if (input[left] == input[right]) {
                    if (right - left > longestPalindromeEnd
                            - longestPalindromeStart) {
                        longestPalindromeStart = left;
                        longestPalindromeEnd = right;
                    }
                }
                else
                    break;
                left--;
                right++;
            }


        }
        // we have the start and end indexes for longest palindrome now
        return in.substring(longestPalindromeStart, longestPalindromeEnd + 1);
    }
public static void main(String args[]){
System.out.println(longestPalindromeString("HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE"));
}

}

这不仅使字符串中的所有回文最长
-Vivek Mishra

0

该解决方案具有O(n ^ 2)的复杂度。O(1)是空间复杂度。

public class longestPalindromeInAString {

        public static void main(String[] args) {
            String a =  "xyMADAMpRACECARwl"; 
            String res = "";
            //String longest = a.substring(0,1);
            //System.out.println("longest => " +longest);
            for (int i = 0; i < a.length(); i++) {
                String temp = helper(a,i,i);//even palindrome
                if(temp.length() > res.length()) {res = temp ;}
                temp = helper(a,i,i+1);// odd length palindrome
                if(temp.length() > res.length()) { res = temp ;}

            }//for
            System.out.println(res);
            System.out.println("length of " + res + " is " + res.length());

        }

        private static String helper(String a, int left, int right) {
            while(left>= 0 && right <= a.length() -1  &&  a.charAt(left) == a.charAt(right)) {
                left-- ;right++ ;
            }
            String curr = a.substring(left + 1 , right);
            System.out.println("curr =>" +curr);
            return curr ;
        }

    }

0

#longest palindrome
s='HYTBCABADEFGHABCDEDCBAGHTFYW123456789987654321ZWETYGDE'
out1=[]
def substring(x):
    for i in range(len(x)):
        a=x[i:]
        b=x[:-i]
        out1.append(a)
        out1.append(b)
        
    return out1

for i in range(len(s)):
    substring(s[i:])    
final=set([item for item in out1 if len(item)>2])
final
palind={item:len(item) for item in final if item==item[::-1]}
print(palind)
sorted(palind.items(),reverse=True, key=lambda x: x[1])[0]

{'DED':3,'123456789987654321':18,'67899876':8,'ABCDEDCBA':9,'456789987654':12,'34567899876543':14,'BCDEDCB':7,'ABA':3,' 5678998765”:10,“ 2345678998765432”:16,“ CDEDC”:5,“ 789987”:6,“ 8998”:4}(“ 123456789987654321”,18)


-1

这是我的算法:

1)将当前中心设为第一个字母

2)同时向左和向右展开,直到找到当前中心附近的最大回文

3)如果您发现的回文大于先前的回文,请对其进行更新

4)将当前中心设置为下一个字母

5)对字符串中的所有字母重复步骤2)至4)

这在O(n)中运行。

希望能帮助到你。


5
考虑字符串“ aaaaaa”。使用您的算法,它以O(n ^ 2)的形式运行。
paislee 2012年

1
我本来以为OP的算法#1是O(n ^ 2)时间,但实际上是O(n ^ 3),所以即使您的算法并没有完全达到可实现的O(n)界限,仍然是一个进步。
j_random_hacker 2012年

那就是经典的N2扩展解决方案。但是,实际上,这与视频中所述的Manacher解决方案非常接近:youtube.com/watch? v=V-sEwsca1ak区别在于第4点。Manacher重用信息可以避免重新扫描已扫描的字母。
v.oddou

-2

参考:Wikipedia.com

我找到的最佳算法,复杂度为O(N)

 import java.util.Arrays;

 public class ManachersAlgorithm {

  public static String findLongestPalindrome(String s) {
    if (s==null || s.length()==0)
      return "";

    char[] s2 = addBoundaries(s.toCharArray());
    int[] p = new int[s2.length]; 
    int c = 0, r = 0; // Here the first element in s2 has been processed.
    int m = 0, n = 0; // The walking indices to compare if two elements are the same
    for (int i = 1; i<s2.length; i++) {
      if (i>r) {
        p[i] = 0; m = i-1; n = i+1;
      } else {
        int i2 = c*2-i;
        if (p[i2]<(r-i)) {
          p[i] = p[i2];
          m = -1; // This signals bypassing the while loop below. 
        } else {
          p[i] = r-i;
          n = r+1; m = i*2-n;
        }
      }
      while (m>=0 && n<s2.length && s2[m]==s2[n]) {
        p[i]++; m--; n++;
      }
      if ((i+p[i])>r) {
        c = i; r = i+p[i];
      }
    }
    int len = 0; c = 0;
    for (int i = 1; i<s2.length; i++) {
      if (len<p[i]) {
        len = p[i]; c = i;
      }
    }
    char[] ss = Arrays.copyOfRange(s2, c-len, c+len+1);
    return String.valueOf(removeBoundaries(ss));
  }

  private static char[] addBoundaries(char[] cs) {
    if (cs==null || cs.length==0)
      return "||".toCharArray();

    char[] cs2 = new char[cs.length*2+1];
    for (int i = 0; i<(cs2.length-1); i = i+2) {
      cs2[i] = '|';
      cs2[i+1] = cs[i/2];
    }
    cs2[cs2.length-1] = '|';
    return cs2;
  }

  private static char[] removeBoundaries(char[] cs) {
    if (cs==null || cs.length<3)
      return "".toCharArray();

    char[] cs2 = new char[(cs.length-1)/2];
    for (int i = 0; i<cs2.length; i++) {
      cs2[i] = cs[i*2+1];
    }
    return cs2;
  }    
}

-5

我的解决方案是:

static string GetPolyndrom(string str)
{
    string Longest = "";

    for (int i = 0; i < str.Length; i++)
    {
        if ((str.Length - 1 - i) < Longest.Length)
        {
            break;
        }
        for (int j = str.Length - 1; j > i; j--)
        {
            string str2 = str.Substring(i, j - i + 1);
            if (str2.Length > Longest.Length)
            {
                if (str2 == str2.Reverse())
                {
                    Longest = str2;
                }
            }
            else
            {
                break;
            }
        }

    }
    return Longest;
}

1
由于和字符串相等()操作,因此在字符串长度上花费了立方时间。它与OP的算法#1基本相同。Substring()==
j_random_hacker 2012年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.