当前位置:网站首页>回溯——131. 分割回文串
回溯——131. 分割回文串
2022-07-26 12:13:00 【向着百万年薪努力的小赵】
1 题目描述
- 分割回文串
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/palindrome-partitioning
2 题目示例
示例 1:
输入:s = “aab”
输出:[[“a”,“a”,“b”],[“aa”,“b”]]
示例 2:
输入:s = “a”
输出:[[“a”]]
3 题目提示
1 <= s.length <= 16
s 仅由小写英文字母组成
4 思路
方法一:回溯 + 动态规划预处理
由于需要求出字符串s的所有分割方案,因此我们考虑使用搜索+回溯的方法枚举所有可能的分割方法并进行判断。
假设我们当前搜索到字符串的第i个字符,且s[0…i-1]位置的所有字符已经被分割成若干个回文串,并且分割结果被放入了答案数组ans中,那么我们就需要枚举下一个回文串的右边界j,使得s[i…j]是一个回文串。
因此,我们可以从i开始,从小到大依次枚举j。对于当前枚举的j值,我们使用双指针的方法判断s[i…j]是否为回文串:如果s[i…j]是回文串,那么就将其加入答案数组ans中,并以j+1作为新的à进行下一层搜索,并在未来的回溯时将s[i…j]从ans中移除。
如果我们已经搜索完了字符串的最后一个字符,那么就找到了—种满足要求的分割方法。
复杂度分析
时间复杂度:o(n 2²),其中n是字符串s的长度。在最坏情况下,s包含n个完全相同的字符,因此它的任意—种划分方法都满足要求。而长度为n的字符串的划分方案数为2n-1= o(2²),每一种划分方法需要O(n)的时间求出对应的划分结果并放入答案,因此总时间复杂度为o(n·2²)。尽管动态规划预处理需要o(n²)的时间,但在渐进意义下小于O(n2²),因此可以忽略。
·空间复杂度:O(n²),这里不计算返回答案占用的空间。数组f需要使用的空间为O(n2),而在回溯的过程中,我们需要使用O(n)的栈空间以及O(n)的用来存储当前字符串分割方法的空间。由于O(n)在渐进意义下小于o(n²),因此空间复杂度为O(n²)。
方法二:回溯 + 记忆化搜索
方法一中的动态规划预处理计算出了任意的s[i…j]是否为回文串,我们也可以将这—步改为记忆化搜索。
复杂度分析
- 时间复杂度:O(n ﹒ 2²),其中n是字符串s的长度,与方法一相同。
- 空间复杂度:o(n²),与方法—相同。
5 我的答案
方法一:回溯 + 动态规划预处理
class Solution {
boolean[][] f;
List<List<String>> ret = new ArrayList<List<String>>();
List<String> ans = new ArrayList<String>();
int n;
public List<List<String>> partition(String s) {
n = s.length();
f = new boolean[n][n];
for (int i = 0; i < n; ++i) {
Arrays.fill(f[i], true);
}
for (int i = n - 1; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
f[i][j] = (s.charAt(i) == s.charAt(j)) && f[i + 1][j - 1];
}
}
dfs(s, 0);
return ret;
}
public void dfs(String s, int i) {
if (i == n) {
ret.add(new ArrayList<String>(ans));
return;
}
for (int j = i; j < n; ++j) {
if (f[i][j]) {
ans.add(s.substring(i, j + 1));
dfs(s, j + 1);
ans.remove(ans.size() - 1);
}
}
}
}
方法二:回溯 + 记忆化搜索
class Solution {
int[][] f;
List<List<String>> ret = new ArrayList<List<String>>();
List<String> ans = new ArrayList<String>();
int n;
public List<List<String>> partition(String s) {
n = s.length();
f = new int[n][n];
dfs(s, 0);
return ret;
}
public void dfs(String s, int i) {
if (i == n) {
ret.add(new ArrayList<String>(ans));
return;
}
for (int j = i; j < n; ++j) {
if (isPalindrome(s, i, j) == 1) {
ans.add(s.substring(i, j + 1));
dfs(s, j + 1);
ans.remove(ans.size() - 1);
}
}
}
// 记忆化搜索中,f[i][j] = 0 表示未搜索,1 表示是回文串,-1 表示不是回文串
public int isPalindrome(String s, int i, int j) {
if (f[i][j] != 0) {
return f[i][j];
}
if (i >= j) {
f[i][j] = 1;
} else if (s.charAt(i) == s.charAt(j)) {
f[i][j] = isPalindrome(s, i + 1, j - 1);
} else {
f[i][j] = -1;
}
return f[i][j];
}
}
边栏推荐
- Ubenwa, a start-up under Mila, received an investment of US $2.5 million to study the AI diagnosis of infant health
- Access database cannot connect
- Understand the string class
- 请问下有人知道FlinkSQL 的 Retrack 在哪里可以指定吗? 网上资料只看到API 代码设
- 2、 Container_
- 尤雨溪向初学者推荐Vite 【为什么使用Vite】
- Pytest interface automated testing framework | introduction to fixture of pytest
- Pytest interface automated testing framework | confitest.py
- Audio and video+
- Pytest interface automation test framework | use decorators to decorate the use cases that need to be run
猜你喜欢

Introduction to FPGA (III) - 38 decoder

Problems and solutions in the learning process of file class

Ds-112 time relay

El form displays two columns per row, with the bottom button centered

扫雷小游戏——轻松玩上瘾(C语言版)

Hit the blackboard and draw the key points: a detailed explanation of seven common "distributed transactions"

Map函数统计字符出现的次数

Understand the string class

使用fastJson中的JSONObject对象简化POST请求传参

JSJ-3/AC220V时间继电器
随机推荐
Question and answer No. 48: geek appointment - construction path of observable system
Li Kai: the interesting and cutting-edge audio and video industry has always attracted me
Redis实现Single单点登入详解
按位与怎么写SQL
【微信小程序】一文读懂,数据请求
What is oom, why it happens and some solutions
大佬们,cdc oracle 怎么设置从指定scn号开始读取,或是怎么设置只读全量的归档,不去读取快
V00 - 年纪大了,想做啥就做啥吧
pytest接口自动化测试框架 | pytest常用插件
On the construction and management of low code technology in logistics transportation platform
You Yuxi recommends vite to beginners [why use vite]
什么是物联网?常见IoT协议最全讲解
腾讯云与智慧产业事业群(CSIG)调整组织架构,成立数字孪生产品部
儿童玩乐场所如何运营?
pytest接口自动化测试框架 | pytest配置文件
剑指 Offer 24. 反转链表
Optical distance sensing chip 4530a combining ambient light, proximity sensing and infrared ranging
羽毛球馆的两个基础设施你了解多少?
Oracle的Windows版本能在linux中使用吗?
尤雨溪向初学者推荐Vite 【为什么使用Vite】