Longest Palindrome 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.

Analyse: Consider every index i in the string. Since there are two possibilities of a palindrome: odd size or even size. If i is in the right middle of a palindrome (odd size), expand the substring starting at i towards left and right part (i - 1 && i + 1, i - 2 && i + 2...), update the result; If i and its neighbor are in the right middle of a palindrome(even size), we only consider its right neighbor because the left neighbor of i‘s right neighbor is i! Expand i and i + 1 towards left and right part (i - 1 && i + 1 + 1, i - 2 && i + 1 + 2, i - 3 && i + 1 + 3...), and update result. Be aware of that all index should be [0, s.size() - 1].

 1 class Solution {
 2 public:
 3     string longestPalindrome(string s) {
 4         string result;
 5         for (int i = 0; i < s.size(); i++) {
 6             // i is in the right middle
 7             int start = i, end = i;
 8             while(start >= 0 && end < s.size() && s[start] == s[end]) {
 9                 start--;
10                 end++;
11             }
12             result = end - start - 1 < result.size() ? result : s.substr(start + 1, end - start - 1);
13
14             // i and i + 1 is in the right middle
15             start = i, end = i + 1;
16             while(start >= 0 && end < s.size() && s[start] == s[end]) {
17                 start--;
18                 end++;
19             }
20             result = end - start - 1 < result.size() ? result : s.substr(start + 1, end - start - 1);
21         }
22         return result;
23     }
24 };
时间: 2024-10-12 15:25:40

Longest Palindrome Substring的相关文章

5.Longest Palindrome substring

/* * 5.Longest Palindrome substring * 2016-4-9 by Mingyang 自然而然的想到用dp来做 * 刚开始自己做的时候分的条件太细,两个index相等,相差小于3,还有其他 * 但这里这个if写的很好,一个代表了所有为true的情况 * 根本不用管为false的情况,因为自然而然为false * */ public String longestPalindrome(String s) { String res = " "; if (s =

最长回文字串 (The longest palindrome substring)

这两天去学了一下,觉得下面那篇文章写的很好,有例子,比较容易懂,所以转一下. 以下内容来自:hihoCoder: 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一连串的字符串,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能分别在这些字符串中找到它们每一个的最长回文子串呢?” 小Ho奇怪的问道:“什么叫做最长回文子串呢?” 小Hi回答道:“一个字符串中连续的一段就是这个字符串的子串,而回文

G.Longest Palindrome Substring

链接:https://ac.nowcoder.com/acm/contest/908/G 题意: A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. For example, ”a”.”aba”.“abba” are palindrome and “abc”.”aabb” are not. Let’s d

[LeetCode] Longest Palindrome 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. 面DP题的考官都是神经病. .(吐槽) 貌似挺easy出回文的题,那么今天细致分析下DP的做法!! 以解我被DP问题虐成渣渣的心碎感觉.如有错误请指出

[LeetCode] Longest Palindrome 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. 面DP题的考官都是神经病..(吐槽) 貌似挺容易出回文的题,那么今天仔细分析下DP的做法!!以解我被DP问题虐成渣渣的心碎感觉.如有错误请指出~ 首先

【算法】最长回文子串 longest palindrome substring

对于字符串S, 要找到它最长的回文子串,能想到的最暴力方法,应该是对于每个元素i-th都向左向右对称搜索,最后用一个数组span 记录下相对应元素i-th为中心的回文子串长度. 那么问题来了: 1. 这样的方法,对于奇回文子串和偶回文子串的处理不一样,比如所"acbca" 和"acbbca" 2. 计算冗余,e.g. "sscssabcccchccccba"中, 自左向右遍历计算的话,会发现, "abcccchccccba"是

LeetCode the longest palindrome substring

回文检测,参考http://blog.csdn.net/feliciafay/article/details/16984031 使用时间复杂度和空间复杂度相对较低的动态规划法来检测,具体的做法 图一 偶数个回文字符情况 图二 奇数个回文字符情况 核心就是如果一个子串是回文,如果分别向回文左右侧扩展一个字符相同,那么回文就向外扩展一位. 实现的代码如下 bool table[1000][1000] = {false}; int sStart = 0; int iLength = 1; int n

[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

【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/ 求最大回文的长度,其实这道题