comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
中等 |
|
给你一个字符串 s
,请你将 s
分割成一些子串,使每个子串都是 回文串 。返回 s
所有可能的分割方案。
示例 1:
输入:s = "aab" 输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a" 输出:[["a"]]
提示:
1 <= s.length <= 16
s
仅由小写英文字母组成
我们可以使用动态规划,预处理出字符串中的任意子串是否为回文串,即
接下来,我们设计一个函数
如果
否则,我们可以从
时间复杂度
class Solution:
def partition(self, s: str) -> List[List[str]]:
def dfs(i: int):
if i == n:
ans.append(t[:])
return
for j in range(i, n):
if f[i][j]:
t.append(s[i : j + 1])
dfs(j + 1)
t.pop()
n = len(s)
f = [[True] * n for _ in range(n)]
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
f[i][j] = s[i] == s[j] and f[i + 1][j - 1]
ans = []
t = []
dfs(0)
return ans
class Solution {
private int n;
private String s;
private boolean[][] f;
private List<String> t = new ArrayList<>();
private List<List<String>> ans = new ArrayList<>();
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];
}
}
this.s = s;
dfs(0);
return ans;
}
private void dfs(int i) {
if (i == s.length()) {
ans.add(new ArrayList<>(t));
return;
}
for (int j = i; j < n; ++j) {
if (f[i][j]) {
t.add(s.substring(i, j + 1));
dfs(j + 1);
t.remove(t.size() - 1);
}
}
}
}
class Solution {
public:
vector<vector<string>> partition(string s) {
int n = s.size();
bool f[n][n];
memset(f, true, sizeof(f));
for (int i = n - 1; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
f[i][j] = s[i] == s[j] && f[i + 1][j - 1];
}
}
vector<vector<string>> ans;
vector<string> t;
function<void(int)> dfs = [&](int i) {
if (i == n) {
ans.push_back(t);
return;
}
for (int j = i; j < n; ++j) {
if (f[i][j]) {
t.push_back(s.substr(i, j - i + 1));
dfs(j + 1);
t.pop_back();
}
}
};
dfs(0);
return ans;
}
};
func partition(s string) (ans [][]string) {
n := len(s)
f := make([][]bool, n)
for i := range f {
f[i] = make([]bool, n)
for j := range f[i] {
f[i][j] = true
}
}
for i := n - 1; i >= 0; i-- {
for j := i + 1; j < n; j++ {
f[i][j] = s[i] == s[j] && f[i+1][j-1]
}
}
t := []string{}
var dfs func(int)
dfs = func(i int) {
if i == n {
ans = append(ans, append([]string(nil), t...))
return
}
for j := i; j < n; j++ {
if f[i][j] {
t = append(t, s[i:j+1])
dfs(j + 1)
t = t[:len(t)-1]
}
}
}
dfs(0)
return
}
function partition(s: string): string[][] {
const n = s.length;
const f: boolean[][] = new Array(n).fill(0).map(() => new Array(n).fill(true));
for (let i = n - 1; i >= 0; --i) {
for (let j = i + 1; j < n; ++j) {
f[i][j] = s[i] === s[j] && f[i + 1][j - 1];
}
}
const ans: string[][] = [];
const t: string[] = [];
const dfs = (i: number) => {
if (i === n) {
ans.push(t.slice());
return;
}
for (let j = i; j < n; ++j) {
if (f[i][j]) {
t.push(s.slice(i, j + 1));
dfs(j + 1);
t.pop();
}
}
};
dfs(0);
return ans;
}
public class Solution {
private int n;
private string s;
private bool[,] f;
private IList<IList<string>> ans = new List<IList<string>>();
private IList<string> t = new List<string>();
public IList<IList<string>> Partition(string s) {
n = s.Length;
this.s = s;
f = new bool[n, n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= i; ++j) {
f[i, j] = true;
}
}
for (int i = n - 1; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
f[i, j] = s[i] == s[j] && f[i + 1, j - 1];
}
}
dfs(0);
return ans;
}
private void dfs(int i) {
if (i == n) {
ans.Add(new List<string>(t));
return;
}
for (int j = i; j < n; ++j) {
if (f[i, j]) {
t.Add(s.Substring(i, j + 1 - i));
dfs(j + 1);
t.RemoveAt(t.Count - 1);
}
}
}
}