5. Longest Palindromic Substring - Unsolved
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
5. Longest Palindromic Substring - Unsolved的更多相关文章
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- No.005:Longest Palindromic Substring
问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
- [LeetCode_5] Longest Palindromic Substring
LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...
- 5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- leetcode-【中等题】5. Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
随机推荐
- php 两个值进行比较的问题
php手册运算符中有介绍: 比较多种类型-- 如var_dump([ ] > 0); // 结果为true 运算数 1 类型 运算数 2 类型 结果 null 或 string string 将 ...
- inception+archery SQL审核平台
关闭防火墙和selinux 宿主机安装mysql,创建archery数据库,并给所有权限,允许远程连接到该数据库 grant all privileges on *.* to 'root'@'%' i ...
- KNN算法应用
import numpy as np# 运算符模块,这里主要用来排序 import operator import matplotlib.pylab as plt def create_dataset ...
- ucore-lab1-练习3report
练习3.分析bootload进入保护模式的过程 0. BIOS通过读取硬盘主引导扇区到内存,并跳转到对应内存中的位置,也就是从’%cs=0 $pc=0x7c00‘进入并执行bootloader,boo ...
- python数据分析之matplotlib学习
本文作为学习过程中对matplotlib一些常用知识点的整理,方便查找. 类MATLAB API 最简单的入门是从类 MATLAB API 开始,它被设计成兼容 MATLAB 绘图函数. from p ...
- ThreaLocal
ThreadLocal概念:线程局部变量,是一种多线程间并发访问变量的解决方案.与其synchronized等加锁的方式不同,ThreadLocal完全不提供锁,而使用以空间换时间的手段,为每个线程提 ...
- 支付宝H5 与网页端支付开发
在日常生活中,我们基本上都是进行微信与支付宝的支付方式尽心支付,这种方式确实大大便利了我们的生活,那么如何在我们的产品中进行微信与支付宝支付的植入开发呢? 我们先进行支付宝的H5与网页端支付开发,这里 ...
- MultiImageSelector 仿微信选择多张图片回调
项目可以去github下载 : https://github.com/lovetuzitong/MultiImageSelector 第0步 把模块 multi-image-selector 作为你的 ...
- Cloud Foundry v2 部署及入门运维
之前写过一个Guide for Cloud Foundry New Teamer.不过似乎已经有些过时,那会实验室主要是针对的CF v1进行的研究,现在已经全面进入V2时代了.所以更新一下关于Clou ...
- 关于js动画简单理解;
1.CSS样式提供了运动 过度的属性:transition 过度的属性值:attr ,time , liner , delay: 值分别是:属性(css),花费的时间,变化的速度(默认匀速), ...