当前位置:网站首页>Offer II 094. Minimum palindrome segmentation
Offer II 094. Minimum palindrome segmentation
2022-07-23 13:08:00 【Three watch ghost】
Title source :https://leetcode.cn/problems/omKAoA/
General meaning :
Give a string , Split the string into substrings , Each substring is a palindrome string , Find the minimum number of segmentation required
Ideas
For strings s[0, j] Substring of range , if s[i, j] It's a palindrome string , that s[0, j] The minimum number of palindrome segmentation is s[0, i - 1] Minimum palindrome segmentation times +1
So we can get the state transition equation :
dp[j] = min(dp[i] + 1), 0 < i < j
Concrete implementation , You can deal with a Boolean array first f[n][n], Used to represent s[i, j] Is it a palindrome string
f[i][j] = (f[i + 1][j - 1] & s[i] = s[j])
Code :
public int minCut(String s) {
int n = s.length();
// Used to represent s[i, j] Whether it is the flag array of palindrome string
boolean[][] f = new boolean[n][n];
// Initialization is true, Mainly is to i >= j The array element of is processed as true
for (int i = 0; i < n; i++) {
Arrays.fill(f[i], true);
}
// Dynamic programming , Completion flag array
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
f[i][j] = f[i + 1][j - 1] && s.charAt(i) == s.charAt(j);
}
}
// Save the minimum palindrome segmentation times
int[] dp = new int[n];
// Initialization is the maximum
Arrays.fill(dp, n);
// Dynamic programming
for (int i = 0; i < n; i++) {
// If the substring s[0, i] It's a palindrome string , Then the number of divisions is 0
if (f[0][i]) {
dp[i] = 0;
continue;
}
for (int j = 0; j < i; j++) {
// When the substring is a palindrome string , Update the minimum number of divisions
if (f[j + 1][i]) {
// State transition equation
dp[i] = Math.min(dp[j] + 1, dp[i]);
}
}
}
return dp[n - 1];
}
边栏推荐
猜你喜欢

Intégrité du signal (si) intégrité de l'alimentation électrique (PI) notes d'apprentissage (32) Réseau de distribution d'énergie (4)

默认路由配置实例学习记录

STP configuration instance learning record

HCIA----06 OSPF

STP 配置实例学习记录

PPP configuration instance learning record

SAR成像之点目标仿真(二)—— Matlab仿真

【离线语音专题④】安信可VC离线语音开发板二次开发语音控制LED灯

Static route configuration instance learning record

记录一次爬虫题库
随机推荐
Matplotlib-实现常见概率分布
默认路由配置实例学习记录
STP 配置实例学习记录
HCIA----02
迷茫、工作没动力? 职业发展没希望? 看这篇文章就够了
用户与组的管理、文件的权限
根据不同时间统计不同类型的数据(存储过程)
SAR成像之点目标仿真(二)—— Matlab仿真
yum安装LNMP服务部署
Telnet 配置实例学习记录
Liveness, readiness and startup probes
常见的cmd命令快速打开程序
CORTEX-A系列处理器
4D毫米波雷达硬件系统架构
力扣 729. 我的日程安排表 I
nfs服务部署笔记
ZABBIX monitoring detailed installation to deployment
将指定秒转为时分秒格式
Install LNMP service deployment using yum
Static routing principle and configuration