返回介绍

solution / 0100-0199 / 0131.Palindrome Partitioning / README_EN

发布于 2024-06-17 01:04:04 字数 5852 浏览 0 评论 0 收藏 0

131. Palindrome Partitioning

中文文档

Description

Given a string s, partition s such that every substring of the partition is a palindrome. Return _all possible palindrome partitioning of _s.

 

Example 1:

Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]

Example 2:

Input: s = "a"
Output: [["a"]]

 

Constraints:

  • 1 <= s.length <= 16
  • s contains only lowercase English letters.

Solutions

Solution 1

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);
      }
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文