当前位置:网站首页>Backtracking - 131. Split palindrome string
Backtracking - 131. Split palindrome string
2022-07-26 12:24:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
- Split palindrome string
Give you a string s, Would you please s Split into some substrings , So that each substring is Palindrome string . return s All possible segmentation schemes .
Palindrome string It's the same string read forward and backward .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/palindrome-partitioning
2 Title Example
Example 1:
Input :s = “aab”
Output :[[“a”,“a”,“b”],[“aa”,“b”]]
Example 2:
Input :s = “a”
Output :[[“a”]]
3 Topic tips
1 <= s.length <= 16
s It's only made up of lowercase letters
4 Ideas
Method 1 : to flash back + Dynamic programming preprocessing
Because you need to find the string s All segmentation schemes , So we consider using search + The backtracking method enumerates all possible segmentation methods and makes judgments .
Let's say we currently find the... Of the string i Characters , And s[0…i-1] All characters of the position have been divided into several palindrome strings , And the segmentation result is put into the answer array ans in , Then we need to enumerate the right boundary of the next palindrome string j, bring s[i…j] It's a palindrome string .
therefore , We can i Start , Enumerate from small to large j. For the current enumeration j value , We use the double pointer method to judge s[i…j] Whether it is palindrome string : If s[i…j] It's a palindrome string , Then add it to the answer array ans in , And j+1 As new à Go to the next level of search , And back in the future will s[i…j] from ans Remove .
If we have searched the last character of the string , So we found — A segmentation method that meets the requirements .
Complexity analysis
Time complexity :o(n 2²), among n Is string s The length of . In the worst case ,s contain n Two identical characters , So its arbitrary — All kinds of division methods meet the requirements . And the length is n The number of partition schemes of the string is 2n-1= o(2²), Each division method requires O(n) Find the corresponding division result and put it into the answer , So the total time complexity is o(n·2²). Although dynamic programming preprocessing needs o(n²) Time for , But in a gradual sense, it is less than O(n2²), So we can ignore .
· Spatial complexity :O(n²), The space occupied by returning the answer is not calculated here . Array f The space to be used is O(n2), And in the process of backtracking , We need to use O(n) Stack space and O(n) The space used to store the current string segmentation method . because O(n) Less than... In a progressive sense o(n²), So the space complexity is zero O(n²).
Method 2 : to flash back + Memory search
The dynamic programming preprocessing in method 1 calculates arbitrary s[i…j] Whether it is palindrome string , We can also put this — Step change to memory search .
Complexity analysis
- Time complexity :O(n ﹒ 2²), among n Is string s The length of , Same as method one .
- Spatial complexity :o(n²), With the method — identical .
5 My answer
Method 1 : to flash back + Dynamic programming preprocessing
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);
}
}
}
}
Method 2 : to flash back + Memory search
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);
}
}
}
// In memory search ,f[i][j] = 0 Indicates no search ,1 Indicates a palindrome string ,-1 Indicates that it is not a palindrome string
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];
}
}
边栏推荐
- 【Mysql约束】
- Implementation of dynamic and static libraries (packaging dynamic and static libraries for others to use)
- 酷早报:7月25日Web3加密行业新闻大汇总
- 海外APP推送(下篇):海外厂商通道集成指南
- 二、容器_
- Pytest interface automated testing framework | introduction to fixture of pytest
- 一些常用的文章写作使用方法和技巧
- What is the Internet of things? The most comprehensive explanation of common IOT protocols
- Ds-24c/dc220v time relay
- yolov7训练危险品识别 pytorch
猜你喜欢

敲黑板画重点:七种常见“分布式事务”详解

STM32驱动HC05蓝牙串口通信模块

什么是物联网?常见IoT协议最全讲解
![[wechat applet] read the article, data request](/img/9a/3b9aef6c5f5735b886252ec830798c.png)
[wechat applet] read the article, data request

Ds-112 time relay

el-form 每行显示两列,底部按钮居中

Use the jsonobject object in fastjason to simplify post request parameter passing

Pytest interface automation test framework | pytest configuration file

一些常用的文章写作使用方法和技巧

Problems and solutions in the learning process of file class
随机推荐
Tencent cloud and smart industry business group (CSIG) adjusted the organizational structure and established the digital twin product department
Beauty salon management system unified management system?
DS-24C/DC220V时间继电器
The map function counts the number of occurrences of characters
Backtracking - question 51 Queen n -- a classic backtracking problem that must be overcome
Network protocol: tcp/ip protocol
HCIP-9.OSPF的各种拓展
数智转型,管理先行|JNPF全力打造“全生命周期管理”平台
STM32驱动HC05蓝牙串口通信模块
Pytorch深度学习快速入门教程 -- 土堆教程笔记(二)
Redis为什么这么快?Redis的线程模型与Redis多线程
结合环境光、接近传感以及红外测距的光距感芯片4530A
回溯——第51题. N皇后——必须攻克的经典回溯难题
Codepoint 58880 not found in font, aborting. Flutter build APK reports an error
Hit the blackboard and draw the key points: a detailed explanation of seven common "distributed transactions"
X.509、PKCS文件格式介绍
You Yuxi recommends vite to beginners [why use vite]
Data query of MySQL (aggregate function)
Introduction to FPGA (III) - 38 decoder
请问下有人知道FlinkSQL 的 Retrack 在哪里可以指定吗? 网上资料只看到API 代码设