Longest Palindromic Substring & Longest Palindromic Subsequence

5. Longest Palindromic Substring

题目链接:https://leetcode.com/problems/longest-palindromic-substring/#/description

题目大意:给定一个字符串s,返回该字符串的最长回文子串。s的最大长度不超过1000.

思路:对于每个子串的中心(可以是一个字符,或者是两个字符的间隙,比如串abc,中心可以是a,b,c,或者是ab的间隙,bc的间隙)往两边同时进行扫描,直到不是回文串为止。假设字符串的长度为n,那么中心的个数为2*n-1(字符作为中心有n个,间隙有n-1个)。

算法复杂度:对于每个中心往两边扫描的复杂度为O(n),所以时间复杂度为O(n^2),空间复杂度为O(1)

代码:

 1 class Solution {
 2 public:
 3     string longestPalindrome(string s) {
 4         if (s == "")
 5             return s;
 6         int start = 0, maxLen = 1;
 7         for (int i = 1; i < s.size(); ++i) {
 8             expandStr(s, i - 1, i, start, maxLen);
 9             expandStr(s, i - 1, i + 1, start, maxLen);
10         }
11         return s.substr(start, maxLen);
12     }
13 private:
14     void expandStr(string &s, int low, int high, int &start, int &maxLen) {
15         while (low >= 0 && high < s.size() && s[low] == s[high]) {
16             --low;
17             ++high;
18         }
19         if (high - low - 1 > maxLen) {
20             start = low + 1;
21             maxLen = high - low - 1;
22         }
23     }
24 };

评测系统上运行结果:

516. Longest Palindromic Subsequence

题目链接:https://leetcode.com/problems/longest-palindromic-subsequence/#/description

题目大意:给定一个字符串s,返回该字符串的最长回文子序列。s的最大长度不超过1000.

思路:使用动态规划。longest数组记录字符串[i,j]范围的最长回文序列,动态转移方程为longest[l][r] = longest[l+1][r-1] + 2 if s[l] == s[r] else longest[l][r] = max(longest[l+1][r], longest[l][r-1]).

算法复杂度:时间复杂度为O(n^2),空间复杂度为O(n^2)

代码:

 1 class Solution {
 2 public:
 3     int longestPalindromeSubseq(string s) {
 4         if (s.empty())
 5             return 0;
 6         vector<vector<int>> longest(s.size(), vector<int>(s.size()));
 7         for (int len = 1; len <= s.size(); ++len)
 8             for (int l = 0; l + len <= s.size(); ++l) {
 9                 int r = l + len - 1;
10                 if (l == r)
11                     longest[l][r] = 1;
12                 else if (s[l] == s[r])
13                     longest[l][r] = longest[l+1][r-1] + 2;
14                 else
15                     longest[l][r] = max(longest[l+1][r], longest[l][r-1]);
16             }
17         return longest[0][s.size() - 1];
18     }
19 };

评测系统上运行结果:

时间: 2024-10-11 06:07:27

Longest Palindromic Substring & Longest Palindromic Subsequence的相关文章

[译]最长回文子串(Longest Palindromic Substring) Part II

[译+改]最长回文子串(Longest Palindromic Substring) Part II 问题:给定字符串S,求S中的最长回文子串. 在上一篇,我们给出了4种算法,其中包括一个O(N2)时间O(1)空间的算法(中心检测法),已经很不错了.本篇将讨论一个O(N)时间O(N)空间的算法,即著名的Manacher算法,并详细说明其时间复杂度为何是O(N). 提示 +BIT祝威+悄悄在此留下版了个权的信息说: 先想想有什么办法能改进中心检测法. 考虑一下最坏的情况.★ 最坏的情况就是各个回文

[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

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之余全部耗在这上面了,只怪自己基础差.本文主要介绍使用Manacher线性算法来求解字符串的最长回文子字符串. 题目 Given a string S, find the longest palindromic substring in S. You may assume that the maxim