[LeetCode5]Longest Palindromic Substring

2023-11-01 15:48

本文主要是介绍[LeetCode5]Longest Palindromic Substring,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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.

定义函数
P[i,j] = 字符串区间[i,j]是否为palindrome.

首先找个例子,比如S="abccb",
S= a b c c b
Index = 0 1 2 3 4

P[0,0] =1 //each char is a palindrome
P[0,1] =S[0] == S[1] , P[1,1] =1
P[0,2] = S[0] == S[2] && P[1,1], P[1,2] = S[1] == S[2] , P[2,2] = 1
P[0,3] = S[0] == S[3] && P[1,2], P[1,3] = S[1] == S[3] && P[2,2] , P[2,3] =S[2] ==S[3], P[3,3]=1
......................
由此就可以推导出规律

P[i,j] = 1 if i ==j
= S[i] ==S[j] if j = i+1
= S[i] == S[j] && P[i+1][j-1] if j>i+1

实现如下:

public String longestPalindrome(String s) {int len = s.length();boolean [][] pal = new boolean[len][len];for(int i=0;i<len;i++){for(int j=0;j<len;j++)pal[i][j] = false;}int start = 0;int end = 0;int mLen = 0;for(int i=0;i<len;i++){for(int j=0;j<i;j++){pal[j][i] = (s.charAt(i)==s.charAt(j) && ((i-j<2) || pal[j+1][i-1]));if(pal[j][i] && mLen<i-j+1){mLen = i-j+1;start = j;end = i;}}pal[i][i] = true;}return s.substring(start, end+1);}

遍历整个字符串,以每个字符串为中心搜索回文并记录

c++

string longestPalindrome(string s) {int startIndex = 0;int len = 0;int sI, eI;for(int i=0; i< s.size()-1; i++){if(s[i] == s[i+1]){sI = i;eI = i+1;Search(s, sI, eI, len, startIndex);}sI = i;eI = i;Search(s, sI, eI, len, startIndex);}if(len ==0)len = s.size();return s.substr(startIndex,len);}void Search(string &s, int sI, int eI, int &len, int &startIndex){int step = 1;while((sI-step)>=0 && (eI+step)<s.size()){if(s[sI-step] != s[eI+step]){ break;}step++;}int wid = eI-sI+2*step-1;if(wid>len){len = wid;startIndex = sI-step +1;}
}


这篇关于[LeetCode5]Longest Palindromic Substring的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/324054

相关文章

usaco 1.2 Palindromic Squares(进制转化)

考察进制转化 注意一些细节就可以了 直接上代码: /*ID: who jayLANG: C++TASK: palsquare*/#include<stdio.h>int x[20],xlen,y[20],ylen,B;void change(int n){int m;m=n;xlen=0;while(m){x[++xlen]=m%B;m/=B;}m=n*n;ylen=0;whi

(nyoj308)substring

Substring 时间限制: 1000 ms  |  内存限制: 65535 KB 难度: 1 描述 You are given a string input. You are to find the longest substring of input such that the reversal of the substring is also a substring of

leetcode#32. Longest Valid Parentheses

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", wh

MySQL中的`SUBSTRING()`和`MID()`函数:精准抽取字符串中的子串

在数据库操作中,经常需要从存储的字符串中提取出特定的部分,比如从用户全名中提取姓氏、从日期字符串中提取年份等。MySQL提供了SUBSTRING()和MID()两个函数,它们的功能几乎完全相同,都是用来从字符串中抽取子串的。本文将详细介绍这两个函数的用法、参数以及在实际场景中的应用。 一、SUBSTRING()和MID()函数的基本语法 1. SUBSTRING()函数 SUBSTRING(

mysql 的函数用法SUBSTRING_INDEX

因为数据库的数据要更新操作,内容是这样的: 这是之前的数据,现在因为需求变更,只需要横杠之前的数据,数据量少可以手动改,但是有几百条的数据,所以找到了一个方法 UPDATE product SET pro_price=SUBSTRING_INDEX(pro_price, '-', 1);  这个SUBSTRING_INDEX就是用来截取的 pro_price是要修改的字段名,然后中间

java常用算法之最长回文子串(Longest Palindromic Substring)

方法一:时间复杂度为O(n^3) public static String longestPalindrome1(String s) {int maxPalinLength = 0;String longestPalindrome = null;int length = s.length();// check all possible sub stringsfor (int i = 0; i

[LeetCode] 300. Longest Increasing Subsequence

题:https://leetcode.com/problems/longest-increasing-subsequence/description/ 题目 Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7

[LeetCode] 409. Longest Palindrome

题:https://leetcode.com/problems/longest-palindrome/description/ 题目 Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with

[LeetCode] 128. Longest Consecutive Sequence

题:https://leetcode.com/problems/longest-consecutive-sequence/description/ 题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should

[LeetCode] 594. Longest Harmonious Subsequence

题:https://leetcode.com/problems/longest-harmonious-subsequence/description/ 题目 We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactl