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. 分布式处理框架MapReduce的深入简出

    1).MapReduce的概述 2).MapReduce 编程模型 3).MapReduce架构 4).MapReduce编程 Google MapReduce论文,论文发表于2004年 Hadoop ...

  2. SQLite4Unity3d

    What's this? When I started with Unity3d development I needed to use SQLite in my project and it was ...

  3. 项目总结02:百度地图js 基本用法介绍

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. unbutu中安装jdk并编写第一个java程序

    第一部分:安装jdk 1.首先在putty控制台中输入如下命令,检验系统中是否已经装有jdk. java -version 如果显式的结果如下图,则说明没有安装. openjdk是在linux下默认安 ...

  5. 封装JedisClient.提供API实现对redis的操作

    需要导包,jedis-2.8.1.jar和博主的序列化工具类SerializeUtils package com.demo.redis; import java.util.ArrayList; imp ...

  6. pom.xml中坐标的组成

    坐标=组织(也就是所谓的公司名称)+项目名称+版本(如果不加范围默认为compile)

  7. Java并发-ConcurrentModificationException原因源码分析与解决办法

    一.异常原因与异常源码分析 对集合(List.Set.Map)迭代时对其进行修改就会出现java.util.ConcurrentModificationException异常.这里以ArrayList ...

  8. jupyter notebook 初步使用配置调整

    jupyter notebook 官方说明 初始部分: 如何打开特定的笔记本? 以下代码应在当前运行的笔记本服务器中打开给定的笔记本,必要时启动一个. jupyter notebook noteboo ...

  9. url中含有%

    Server.UrlEncode(“参数”)也可以使用javascript 的编码方式href="页面?name=encodeURI("参数")传送页代码编码 接收页代码 ...

  10. PAT 1049 数列的片段和(20)(代码+思路分析)

    1049 数列的片段和(20)(20 分) 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2 ...