leetcode Longest Palindromic Substring Part II

本文介绍了一种高效算法来查找给定字符串中的最长回文子串,通过利用回文的对称特性实现O(n)的时间复杂度。文章详细解释了算法的工作原理,并提供了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

 

There is little to say about the O(n^2) solution. 

The O(n) solution is by virtue of symmetric property. 

if P[ i' ] ≤ R – i,
then P[ i ] ← P[ i' ]
else P[ i ] ≥ P[ i' ]. (Which we have to expand past the right edge (R) to find P[ i ].

 

If the palindrome centered at i does expand past R, we update C to i, (the center of this new palindrome), and extend R to the new palindrome’s right edge.

 

So the code is :

 

class Solution {
 public:
  string parse(string& s) {
    string res = "^#";
    for (int i = 0; i < s.length(); ++i)
      res += s.substr(i,1) + "#";
    res += "$";
    return res;
  }
  string longestPalindrome(string s) {
    if (s.length() <= 1)
      return s;
    string T = parse(s);
    int n = T.length();
    vector<int> P(n,0);
    int i, j, C = 0, R = 0, ii;
    for (i = 1; i < n - 1; ++i) {
      ii = C*2 - i;
      P[i] = (R - i) > 0 ? min(P[ii], R - i) : 0;
      while (T[i - 1 - P[i]] == T[i + 1 + P[i]])
        ++P[i];
      if (i + P[i] > R){
        R = i + P[i];
        C= i;    
      }
    }
    int max = 0;
    int maxIndex = 1;
    for (i = 1; i < n - 1; ++i) {
      if (P[i] > max) {
        max = P[i];
        maxIndex = i;
      }
    }
    return s.substr( (maxIndex - max - 1 ) / 2,max);
  }
};

Add some explanations where the complexity is O(n), for the step if (i+P[i] > R):

1. If i+P[i] <= R, the while loop will execute only once because of the sysmetric property

2. If i+P[i]>R, P[i] starts from R-i. The updates for R is linear. 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值