Longest Palindromic Substring 解答

Question

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.

Solution

Palindrome类型的题目的关键都是这个递推表达式:

dp[i][j] = (s[i] == s[j]) && dp[i + 1][j - 1]

逆向思维,于是我们想到由 dp[i][j] 可以推出以下:

dp[i - 1][j + 1], dp[i - 2][j + 2], dp[i - 3][j + 3]...

这题的一个思路是用DP构造2D Array,参见 Palindrome Subarray

另一个思路也是借助了DP的思想,时间复杂度仍是O(n2),但是空间复杂度是O(1)

我们对于每个起点遍历,找以 1. 它为中心的最长对称子序列 2. (如果它和它的邻居相等)它和它的邻居为中心的最长对称子序列

代码如下

 1 class Solution(object):
 2     def spand(self, s, start, end):
 3         length = len(s)
 4         while start >= 0 and end < length:
 5             if s[start] == s[end]:
 6                 start -= 1
 7                 end += 1
 8             else:
 9                 break
10         return s[start + 1: end]
11
12     def longestPalindrome(self, s):
13         """
14         :type s: str
15         :rtype: str
16         """
17         length = len(s)
18         result = s[0]
19         for i in range(length - 1):
20             # sub-length is odd
21             result1 = self.spand(s, i, i)
22             if len(result) < len(result1):
23                 result = result1
24             # sub-length is even
25             if s[i] == s[i + 1]:
26                 result2 = self.spand(s, i, i + 1)
27                 if len(result) < len(result2):
28                     result = result2
29         return result
时间: 2024-08-08 05:22:19

Longest Palindromic Substring 解答的相关文章

【翻译】Longest Palindromic Substring 最长回文子串

原文地址: http://www.cnblogs.com/zhxshseu/p/4947609.html%20 转载请注明出处:http://www.cnblogs.com/zhxshseu/p/4947609.html 问题描述:Given a string S, find the longest palindromic substring in S. 这道题目是一个经典的动态规划DP http://challenge.greplin.com/问题,在面试中经常会被问到.为什么?因为这个问题可

LeetCode 题解之 5. Longest Palindromic Substring

5. Longest Palindromic Substring 题目描述和难度 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设?s 的最大长度为1000. 示例 1: 输入: "babad"输出: "bab"注意: "aba"也是一个有效答案. 示例 2: 输入: "cbbd"输出: "bb" 题目难度:中等. 英文网址:5. Longest Palindromic Substri

[string]Longest Palindromic Substring

Total Accepted: 82026 Total Submissions: 379898 Difficulty: Medium 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. Subscrib

LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

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. 玩了两天dota2,罪过罪过,还是应该老老实实刷题啊. 题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以

5. Longest Palindromic Substring - Unsolved

https://leetcode.com/problems/longest-palindromic-substring/#/description Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: &

[Leetcode] Longest palindromic substring 最长回文子串

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. 做这道题之前要先了解什么是回文子串.回文串通俗的解释是,分别从字符串两端开始遍历,得到的结果相同,如"abba",从两端的遍历结果都是:&q

【leedcode】 Longest Palindromic Substring

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. https://leetcode.com/problems/longest-palindromic-substring/ 求最大回文的长度,其实这道题

5. Longest Palindromic Substring

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. 思路:(beat 2.63%) 以一个位置为准,考察两侧相同距离是否相同.这样会导致"aaaaaaaaaaaaaaaaaaaaaaaaaa...&qu

LeetCode #5 Longest Palindromic Substring (M)

[Problem] 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. [Analysis] 这题的思路有很多种,网上也有各种讨论.这里我采用的是个人觉得比较好理解的一种利用Dynamic Progra