当前位置:网站首页>Interview high frequency algorithm question --- longest palindrome substring
Interview high frequency algorithm question --- longest palindrome substring
2022-06-11 16:13:00 【Java ape~】
Catalog
subject
Topic link : Longest text substring
Give a string s, find s The longest palindrome substring in

Topic analysis
So-called Palindrome Namely “ Fog locks the mountain ”,“ Day after day, tail water with sky ” , Namely Positive and negative are the same
such as “abcdcba”, We found that this is a palindrome string ,“bcdcb” It's also a palindrome string ,“cdc” It's also a palindrome string ,“d” It's also a palindrome string , We can find a law : A palindrome string is also a palindrome string without a header and a tail
So we can use Dynamic programming To solve this problem
F(i,j) Representation string s Of the i To the first j A string of characters ,F(i,j)=true Indicates a palindrome string ,F(i,j)=false Indicates that it is not a palindrome string
From the above explanation, we can know if F(i+1,j-1) Palindrome string , And satisfy the string s Of the i And the j The characters are equal , that F(i,j) Is a palindrome string
So we can write dynamic programming State transition equation :F(i,j) = F(i+1,j-1) && (Si==Sj), This equation means :F(i+1,j-1) Is a palindrome string and s Of the i Characters and the j The characters are equal F(i,j) Is the palindrome string
Code implementation
public class Solution {
public String longestPalindrome(String s) {
int len = s.length();
int maxLen = 1; // Maximum length of palindrome substring
int begin = 0; // The starting position of the palindrome substring
if(len < 2){ // If the string has only one element , So it must be palindrome string
return s;
}
//dp[i][j] Express s Of the i One to the first j Whether a character is a palindrome string
boolean[][] dp = new boolean[len][len];
for(int i = 0;i < len;i++){ // The length is 1 The substring of is palindrome string
dp[i][i] = true;
}
char[] arr = s.toCharArray();
for(int L = 2;L <= len;L++){ //L Is the length of the substring
for(int i = 0;i < len;i++){ //i Is the leftmost subscript of the substring
int j = L + i - 1; //j Is the rightmost subscript of the substring
if(j >= len){ // The subscript crossing the line , Exit loop
break;
}
if(arr[i] != arr[j]){ // The first and last characters of the substring are not equal , Definitely not a palindrome string
dp[i][j] = false;
}else {
// When the first element of the substring is equal
if(j - i < 3){ // If the substring length is 2,3 when , It's a palindrome string
dp[i][j] = true;
}else {
dp[i][j] = dp[i+1][j-1]; // if dp[i+1][j-1] A palindrome string is a palindrome string
}
}
if(dp[i][j] && j-i+1>maxLen){ // Update the length of the maximum palindrome substring and the starting position of the substring
maxLen = j-i+1;
begin = i;
}
}
}
return s.substring(begin,begin+maxLen); // from s Intercepts the largest palindrome substring in and returns
}
}Code parsing



Complexity analysis
Time complexity : Length of external loop substring , The starting value of the inner loop substring , Double loop nesting , The time complexity is O(n^2)
Spatial complexity : Storage space for dynamic planning dp[len][len], So the complexity of space is O(n^2)
边栏推荐
- Database design recommendations
- Interview classic question: how to do the performance test? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]
- Factory calibrated gravity: working principle and installation position of carbon monoxide sensor, calibration standard description
- 09 Minimum Spanning Tree highway
- Nielseniq announces appointment of Tracey Massey as chief operating officer
- 书籍《阅读的方法》读后感
- Deep separable convolution
- Pytest测试框架基础篇
- High concurrency pseudo sharing and cache line filling (cache line alignment) (@contained)
- laravel 8 通过 任务调度 实现 数据库备份
猜你喜欢

List和Set存取元素的差异

High concurrency pseudo sharing and cache line filling (cache line alignment) (@contained)

(湖南科技大学oj作业)问题 G: 串的模式匹配

laravel 2020-01-01T00:00:00.000000Z 日期转化
![[sword finger offer] 21 Adjust array order so that odd numbers precede even numbers](/img/ba/8fa84520bacbc56ce7cbe02ee696c8.png)
[sword finger offer] 21 Adjust array order so that odd numbers precede even numbers

Cloud data management will break the island of storage and the island of team

C# 启动一个外部exe文件,并传入参数

Opengauss AI capability upgrade to create a new AI native database

Customized thread communication (lock) of JUC

【剑指Offer】22.链表中倒数第K节点
随机推荐
1267_FreeRTOS启动第一个任务接口prvPortStartFirstTask实现分析
Laravel 8 realizes database backup through task scheduling
leetcode417. 太平洋大西洋水流问题(中等)
laravel 8 使用passport 进行Auth验证及颁发token
Frontier technology exploration deepsql: in Library AI algorithm
Easy to use GS_ Dump and GS_ Dumpall command export data
想学好ArrayList,看完这篇就够了
Golang map basic operations
Nat commun | language model can learn complex molecular distribution
TC8:UDP_ MessageFormat_ 01-02
PyQt5 使QPlainTextEdit控件支持行号显示
What happened to the frequent disconnection of the computer at home
How the autorunner automated test tool creates a project -alltesting | Zezhong cloud test
one hundred and twenty-three thousand one hundred and twenty-three
推开混合云市场大门,Lenovo xCloud的破局之道
[从零开始学习FPGA编程-17]:快速入门篇 - 操作步骤2-5- VerilogHDL硬件描述语言符号系统与程序框架(软件程序员和硬件工程师都能看懂)
Database resource load management (Part 2)
Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library
Application of AI in index recommendation
再聊数据中心网络