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. 一:怎样运行python程序

    运行python程序有三种方法: 1,在命令行运行python代码:C:/python27>python hello.py   [注:如果没有把PATH环境变量设置为包含这一路径,要确保输入到了 ...

  2. zabbix_server.conf 详解

    # This is a configuration file for Zabbix server daemon # To get more information about Zabbix, visi ...

  3. vue-cli 配置 proxyTable pathRewrite

    vue-config-index.js中,proxyTable中的pathRewrite有什么用呢? 首先,在ProxyTable模块中设置了‘/api’,target中设置服务器地址,也就是接口的开 ...

  4. DC-学习

    1.DC概论一:setup time, hold time之一 http://www.blogbus.com/bb2hh-logs/20463915.html 2.DC概论二:fanout, skew ...

  5. 前端框架(kraken、Express、Node、MVC)

    You know my loneliness is only kept for you, my sweet songs are only sang for you. 前端框架相关知识记录. krake ...

  6. WEB框架Django之ORM操作

    一 ORM的简介 MVC或者MVC框架中包括的一个重要部分就是ORM,它实现了数据模型与数据库的解耦. 即数据模型的设计不需要依赖于特定的数据库,通过简单的配置可以轻松更换数据库,这可以大大减少开发人 ...

  7. redis 哨兵(sentinel)

    redis哨兵 哨兵自动故障转移 自动通知应用最新master信息 无需担心,master挂了,程序不需要修改IP啥的,由哨兵自动完成 修改sentinel.conf protected-mode n ...

  8. python提取分析表格数据

    #/bin/python3.4# -*- coding: utf-8 -*- import xlrd def open_excel(file="file.xls"): try: d ...

  9. Can't find bundle for base name test.properties, locale zh_CN

    这个问题花了我好长时间,网上搜了一个答案:http://verran.iteye.com/blog/44357 是将properties文件放在新建的文件夹下,如config,然后将config加入到 ...

  10. POJ3417 Network

    一道LCA+树上差分 原题链接 显然每一条新增边都会导致环. 如果试着举些例子的话,很容易发现割掉非环上的边,则割掉其他任意一条新增边都可达成目标:若割掉的原有边是一个环上的边,那么只有割掉导致这个环 ...