当前位置:网站首页>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];
}
}
边栏推荐
- Pytest interface automated testing framework | pytest obtains execution data, and pytest disables plug-ins
- Shell变量和引用
- RFID的工作原理
- UE5 官方案例Lyra 全特性详解 7.资源管理
- C语言文件知识点
- MySQL之数据查询(聚合函数)
- 代码报错解决问题经验之二:YOLOv5中的test报错
- .eslintrc.js配置说明
- Real time synchronization and conversion of massive data based on Flink CDC
- Overseas app push (Part 2): Channel Integration Guide for overseas manufacturers
猜你喜欢

How does the chain store cashier system help shoe stores manage their branches?

Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (medium)

Real time synchronization and conversion of massive data based on Flink CDC

Optical distance sensing chip 4530a combining ambient light, proximity sensing and infrared ranging

Backtracking - question 51 Queen n -- a classic backtracking problem that must be overcome

Pytest interface automated testing framework | common plug-ins of pytest

HCIP-9.OSPF的各种拓展

MySQL组合索引(多列索引)使用与优化

The.Net webapi uses groupname to group controllers to render the swagger UI
![[map] universal map usage & two ways of fuzzy query](/img/ec/e95e4057af2b9133786bb4103d2daa.png)
[map] universal map usage & two ways of fuzzy query
随机推荐
HTAP comes at a price
Detailed explanation of Legendre transformation and conjugate function
Interview JD T5, was pressed on the ground friction, who knows what I experienced?
一些常用的文章写作使用方法和技巧
.NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI
Ssj-21b time relay
空洞卷积详解(输入输出大小分析)
Uniapp H5, APP references external online JS
回溯——131. 分割回文串
Flutter's learning path
回溯——491. 递增子序列
Use the jsonobject object in fastjason to simplify post request parameter passing
Redis主从复制原理
二、容器_
Implementation of dynamic and static libraries (packaging dynamic and static libraries for others to use)
Introduction to FPGA (I) - the first FPGA project
Pytest interface automation test framework | execute use cases through markup expressions
使用fastJson中的JSONObject对象简化POST请求传参
Transactional事务传播行为?
Pytest interface automated testing framework | introduction to fixture of pytest