当前位置:网站首页>Palindrome related topics
Palindrome related topics
2022-07-23 14:32:00 【ATTACH_ Fine】
647. Palindrome string
subject
Give you a string s , Please count and return this string Palindrome string Number of . Palindrome characters Is reading the same string as reading it backwards . A substring is a sequence of consecutive characters in a string .
A substring with different start or end positions , Even if it's made up of the same characters , It's also seen as a different substring .
Example :
Ideas
1. determine dp Array (dp table) And the meaning of subscripts
Boolean type dp[i][j]: Express Range [i,j] ( Note that the left is closed and the right is closed ) Whether the substring of is a palindrome substring , If it is dp[i][j] by true, Otherwise false.
2. Determine the recurrence formula
s[i] And s[j] It's not equal return false;
s[i] And s[j] equal
Situation 1 : Subscript i And j identical , The same character, for example a, Palindromes, of course i==j
Situation two : Subscript i And j The difference is 1, for example aa, It's also palindrome substring j - i == 1
Situation three : Subscript :i And j The difference is greater than 1 When , for example cabac, here s[i] And s[j] It's the same , Let's see i To j Whether the interval is a palindrome substring depends on aba Is palindrome enough , that aba The interval is i+1 And j-1 Section , Whether this interval is palindrome depends on **dp[i + 1][j - 1]** Is it true.
3. initialization
4. Determine the traversal order
Case 3 is based on dp[i + 1][j - 1] Is it true, In the face of dp[i][j] Assign a value true Of .dp[i + 1][j - 1] stay dp[i][j] The lower left corner of the , Pictured :
Be sure to go from bottom to top , Traverse from left to right , This guarantee dp[i + 1][j - 1] It's all calculated .
Code
class Solution {
public int countSubstrings(String s) {
// Dynamic programming dp[i][j] representative [i,j] Whether the palindrome character is in the interval
//dp[i][j] from dp[i-1][j+1] decision
int len = s.length();
boolean[][] dp = new boolean[len][len];
int res = 0;
// From bottom to top , Traverse from left to right
for(int i = len-1; i >= 0; i--){
for(int j = i; j < len; j++){
if(s.charAt(i) == s.charAt(j)){
//(s.charAt(i)==s.charAt(j) when , When the number of elements is 1,2,3 Time , Must be a palindrome substring
if(j - i <= 1){
res++;
dp[i][j] = true;
}else if(dp[i+1][j-1] == true){
res++;
dp[i][j] = true;
}
}
}
}
return res;
}
}
516. The longest palindrome subsequence
Give you a string s , Find out The longest palindrome subsequence , And return the length of the sequence .
Subsequence is defined as : Without changing the order of the remaining characters , A sequence formed by deleting some characters or not deleting any characters .
Example :
Ideas
1. determine dp Array (dp table) And the meaning of subscripts
dp[i][j]: character string s stay **[i, j] Range ** The length of the longest palindrome subsequence in is dp[i][j].
2. Determine the recurrence formula
In the question of judging palindrome substrings , The key logic is to look at s[i] And s[j] Are they the same? .
(1) If s[i] And s[j] identical , that dp[i][j] = dp[i + 1][j - 1] + 2;
(2) If s[i] And s[j] inequality , explain s[i] and s[j] Join in at the same time Does not increase [i,j] The length of the interval palindrome substring , Then join s[i]、s[j] See which can form the longest palindrome subsequence .
Join in s[j] The length of palindrome subsequence of is dp[i + 1][j].
Join in s[i] The length of palindrome subsequence of is dp[i][j - 1].
dp[i][j] It must be the largest , namely :dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
3. initialization
First of all, consider When i and j identical The situation of , From the recursive formula :dp[i][j] = dp[i + 1][j - 1] + 2; It can be seen that The recursive formula dp[i][j] Yes, it cannot be calculated i and j At the same time .
So you need to initialize it manually , When i And j identical , that dp[i][j] It must be equal to 1 Of , namely : The length of the palindrome subsequence of a character is 1.
Other situations dp[i][j] For the initial 0 Just go , So the recurrence formula :dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); in dp[i][j] Will not be overwritten by the initial value .
4. Determine the traversal order
So traverse i Be sure to traverse from bottom to top , In this way, we can guarantee , The data in the next row is calculated .
Code
class Solution {
public int longestPalindromeSubseq(String s) {
// Dynamic programming dp[i][j] On behalf of the [i,j] The length of the longest palindrome subsequence on
int len = s.length();
int[][] dp = new int[len+1][len+1];
// From bottom to top , Traverse from left to right
for(int i = len-1; i >= 0; i--){
for(int j = i+1; j < len; j++){
// When i and j Initialize at the same time
dp[i][i] = 1;
if(s.charAt(i) == s.charAt(j)){
dp[i][j] = dp[i+1][j-1] + 2;
}else{
dp[i][j] = Math.max(dp[i][j-1],dp[i+1][j]);
}
}
}
return dp[0][len-1];
}
}
边栏推荐
- Flat style feedback form page
- npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
- shell跑的时候需要的需要了解命令
- Cool code rain dynamic background registration page
- 全志F1C100S/F1C200S学习笔记(13)——LVGL移植
- Chicken and egg, products and Strategies
- Experience in developing large crawlers in requests Library
- webstrom ERROR in [eslint] ESLint is not a constructor
- JS software unloading prompt expression changes with the mouse JS special effect
- OKRK3399开发板预留I2C4挂载EEPROM
猜你喜欢

C语言实现课堂随机点名系统

Canvas 从入门到劝朋友放弃(图解版)

wacom固件更新错误123,数位板驱动更新不了

在使用 VScode 进行代码格式化后,保存发现代码又变乱了,怎么办?vs去掉格式化

买卖股票的最佳时机

炫酷代码雨动态背景注册页面

How can manual testing turn to automated testing? Byte 5 years of automation experience talk about

npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.

ThreadLocal interview Kills 11 consecutive questions

【模电复习——二极管】
随机推荐
CPU,内存,磁盘速度比较
Canvas eraser function
Shell practice: one click start process, close process and view process status
全志F1C100S/F1C200S学习笔记(13)——LVGL移植
视觉与智能学习近期期刊阅读与相关知识学习
How can manual testing turn to automated testing? Byte 5 years of automation experience talk about
Offline data interoperability
Authing 支持 Zadig 啦!云原生用户统一认证快速对接
338. 比特位计数
买卖股票的最佳时机
链下数据互操作
requests库大型爬虫开发经验
鸡与蛋,产品与策略
Shell实践:一键启动进程、关闭进程、查看进程状态
JS software unloading prompt expression changes with the mouse JS special effect
第2章 基础查询与排序
Spotlight light box JS plug-in full screen enlarged picture
ValidationError: Invalid options object. Dev Server has been initialized using an options object th
js日历样式饼图统计插件
Description of test platform and hardware design