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: "aba" is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

Sol 1: 

DP.

State transition equation:

 
 

The state is f(i, j) , it means the state transition equation in interval [i,j]. 

The next status is based on the previous status. We expand inner block by 1 char at a time. 

public class Solution {
public String c(String s) { // DP
// Time O(n2) Space O(n^2)
final int n = s.length();
final boolean [][] f = new boolean [n][n];
int maxLen = 1, start = 0; // The start of the longestPalindrome for (int i = 0; i < n; i++){
f[i][i] = true;
for(int j = 0; j < i; j++){
f[j][i] = (s.charAt(j) == s.charAt(i) && (i - j < 2 || f[j+1][i-1]));
if (f[j][i] && maxLen < (i-j+1)){
maxLen = i - j + 1;
start = j;
}
}
} return s.substring(start, start + maxLen); }
}

Sol 2:

Manacher algorithm in Python O(n)

https://discuss.leetcode.com/topic/10484/manacher-algorithm-in-python-o-n

WIKI

https://en.wikipedia.org/wiki/Longest_palindromic_substring

5. Longest Palindromic Substring - Unsolved的更多相关文章

  1. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  2. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  3. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  4. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  5. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  7. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

  8. 5. Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. leetcode-【中等题】5. Longest Palindromic Substring

    题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...

随机推荐

  1. 第十一章 串 (c2)KMP算法:查询表

  2. const 全面理解

    问题1:const int a : 和  int  const  a :的区别 定义一个变量: 类型描述符 + 变量名 类型描述符包括类型修饰符和数据类型. 类型修饰符有:short  long  u ...

  3. 最小生成树 prime算法 UVALive - 6437

    题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...

  4. 调用高德地图API(热力图)详解

    具体脚本语言如下: <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  5. Qt的pro文件--项目配置的部分字段

    Qt项目配置的部分字段: 库: LIBS += -L /usr/local/lib -lpcap INCLUDEPATH += /usr/local/include/

  6. 使用命令行执行sql文件

    用Navicat 导入sqlserver数据库时,出现了out of memory异常,百度无果,想起之前用命令行导入过,再次试了一下成功 用法: 打开执行命令: sqlcmd -S localhos ...

  7. C++ 读取文本文件内容到结构体数组中并排序

    成绩排行:从Score.txt文件读取学生信息,对其进行排序,按回答题数从大到小排,若相等,按分数从小到大排 #include<iostream> #include<fstream& ...

  8. delete,truncate 和 delete之间的区别

    1.首先看下语法定义: drop table_name truncate table_name delete table_name [where column_name = value] 2.各个删除 ...

  9. 弹框alertView

    // 创建一个弹框UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“标题” message:@“显示的具体内容” delegate:s ...

  10. golang 创建一个简单的资源池,重用资源,减少GC负担

    package main; import ( "sync" "errors" "fmt" ) //代码参考<Go语言实战>中第7 ...