当前位置:网站首页>Count the number of different palindrome subsequences in the string
Count the number of different palindrome subsequences in the string
2022-06-29 00:52:00 【GreyZeng】
Count the number of different palindrome subsequences in the string
author :Grey
Original address : Count the number of different palindrome subsequences in the string
Problem description
Given a string str, Of course, many subsequences can be generated , Returns how many subsequences are palindromes , Empty sequences are not palindromes , such as ,str = “aba” Palindrome subsequence has
{a}:0 In position a
{a}:2 In position a
{a,a}
{b}
{a,b,a}
So back 5.
Violence solution
Enumerate each subsequence , Then determine whether the subsequence is a palindrome , The code is as follows
public static int ways1(String str) {
if (str == null || str.length() == 0) {
return 0;
}
char[] s = str.toCharArray();
char[] path = new char[s.length];
return process(str.toCharArray(), 0, path, 0);
}
// Enumerate each location to or from , Get the number of palindrome subsequences
public static int process(char[] s, int si, char[] path, int pi) {
if (si == s.length) {
return isP(path, pi) ? 1 : 0;
}
int ans = process(s, si + 1, path, pi);
path[pi] = s[si];
ans += process(s, si + 1, path, pi + 1);
return ans;
}
// Judge path In a string 0...pi-1 Is it a palindrome
public static boolean isP(char[] path, int pi) {
if (pi == 0) {
return false;
}
int L = 0;
int R = pi - 1;
while (L < R) {
if (path[L++] != path[R--]) {
return false;
}
}
return true;
}
Dynamic programming solution
Define a two-dimensional array dp, The array length is assumed to be n
int[][] dp = new int[n][n]
dp[i][j] The meaning is : character string [i...j] The maximum number of palindrome subsequences that can be obtained in the interval , therefore ,dp[0][n-1] The value of is the final answer we need to find .
according to dp The meaning of array , We can get , Two dimensional matrix dp The values on the diagonals in are all 1, The position above the diagonal can also be easily obtained , See note below
// The values on the diagonal are all 1
for (int i = 0; i < n; i++) {
dp[i][i] = 1;
}
// The position above the diagonal , namely dp[i][i+1] Value on
// If str[i] == str[i+1], such as :aa, There are three palindrome subsequences , Namely {a},{a},{aa}
// If str[i] != str[i+1], such as :ab, There are two palindrome subsequences , Namely {a},{b}
for (int i = 0; i < n - 1; i++) {
dp[i][i+1] = str[i] == str[i+1] ? 3 : 2;
}
Next, consider the general location dp[i][j], There can be several situations as follows :
Situation 1 ,i The character of position is discarded , that dp[i][j] = dp[i+1][j];
Situation two ,j The character of position is discarded , that dp[i][j] = dp[i][j-1];
Based on case one and case two ,
dp[i][j] = dp[i+1][j] + dp[i][j-1]
This is the time , In fact, it is a heavy part , The heavy part is dp[i+1][j-1], therefore
dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1]
If str[i] == str[j], Then there is case three , Case III medium ,str[i] and str[j] But keep it all , And interval [i+1...j-1] Form palindrome string ,str[i] It can also be used alone with str[j] Form a palindrome string . therefore , Situation three
// dp[i][j] In two parts
// among :
// The first part :dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] Express i Location and j When the position is abandoned , The number of answers obtained
// The second part :dp[i + 1][j - 1] + 1 In case 3 , Use at the same time str[i] and str[j] The number of answers from the position
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + dp[i + 1][j - 1] + 1
Dynamic programming solution of the complete code
public static int ways2(String s) {
if (s == null || s.length() == 0) {
return 0;
}
char[] str = s.toCharArray();
int n = str.length;
int[][] dp = new int[n][n];
// The diagonals are 1
for (int i = 0; i < n; i++) {
dp[i][i] = 1;
}
for (int i = 0; i < n - 1; i++) {
dp[i][i + 1] = str[i] == str[i + 1] ? 3 : 2;
}
for (int i = n - 3; i >= 0; i--) {
for (int j = i + 2; j < n; j++) {
// subtract dp[i+1][j-1] It's because it's repeated
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1];
if (str[i] == str[j]) {
// dp[i][j] In two parts
// among :
// The first part :dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] Express i Location and j When the position is abandoned , The number of answers obtained
// The second part :dp[i + 1][j - 1] + 1 In case 3 , Use at the same time str[i] and str[j] The number of answers from the position
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + dp[i + 1][j - 1] + 1;
}
}
}
return dp[0][n - 1];
}
Similar problems
LeetCode 730. Statistics of different palindrome subsequences
more
边栏推荐
- [staff] pedal mark (step on pedal ped mark | release pedal * mark | corresponding pedal command in MIDI | continuous control signal | switch control signal)
- 统计字符串中不同回文子序列的个数
- Bug risk level
- 学习通否认 QQ 号被盗与其有关:已报案;iPhone 14 量产工作就绪:四款齐发;简洁优雅的软件早已是明日黄花|极客头条
- [leetcode] 522. Longest special sequence II violence + double pointer
- If you can play these four we media operation tools, the sideline income of 6000+ is very easy
- BMFONT制作位图字体并在CocosCreator中使用
- 【leetcode】1719. Number of schemes for reconstructing a tree
- Blazor University (34) forms - get form status
- 利用verilogA模块采样
猜你喜欢

架构实战营|模块5

滑环电机是如何工作的

be based on. NETCORE development blog project starblog - (13) add friendship link function
![[eight part essay] MySQL](/img/8e/719149fb49f1850baf5bab343955bf.jpg)
[eight part essay] MySQL
【架构师(第三十八篇)】 服务端开发之本地安装最新版 MySQL 数据库

Redis常用命令手册

Daily question 1: the number of numbers in the array

BMFONT制作位图字体并在CocosCreator中使用

手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??
![[MCU club] design of classroom number detection based on MCU [simulation design]](/img/87/6dc27c90c9f9d26a6338b3618e974b.jpg)
[MCU club] design of classroom number detection based on MCU [simulation design]
随机推荐
Précautions d'installation et d'utilisation des joints rotatifs
搭建单机 nacos 负载均衡ribbon 轮询策略 权重2种方式
Nodejs安装和下载
【SV 基础】queue 的一些用法
[200 opencv routines] 101 adaptive median filter
It is safer for individuals to choose a securities company to open an account when buying interbank certificates of deposit
深度优先搜索实现抓牛问题
pinhole camera model
[SV basics] some usage of queue
Is it safe to open an account on great wisdom
由背景图缓存导致的canvas绘图跨域问题
[MCU club] design of GSM version of range hood based on MCU [physical design]
Is l1-031 too fat (10 points)
Structure of the actual combat battalion | module 5
分析框架——用户体验度量数据体系搭建
How to calculate the income tax of foreign-funded enterprises
使用.Net驱动Jetson Nano的OLED显示屏
Daily practice: delete duplicates in the ordered array
674. longest continuous increasing sequence
Two ways to set up a single Nacos load balancing ribbon polling policy weight