返回介绍

solution / 1200-1299 / 1278.Palindrome Partitioning III / README_EN

发布于 2024-06-17 01:03:21 字数 4914 浏览 0 评论 0 收藏 0

1278. Palindrome Partitioning III

中文文档

Description

You are given a string s containing lowercase letters and an integer k. You need to :

  • First, change some characters of s to other lowercase English letters.
  • Then divide s into k non-empty disjoint substrings such that each substring is a palindrome.

Return _the minimal number of characters that you need to change to divide the string_.

 

Example 1:

Input: s = "abc", k = 2
Output: 1
Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome.

Example 2:

Input: s = "aabbc", k = 3
Output: 0
Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome.

Example 3:

Input: s = "leetcode", k = 8
Output: 0

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def palindromePartition(self, s: str, k: int) -> int:
    n = len(s)
    g = [[0] * n for _ in range(n)]
    for i in range(n - 1, -1, -1):
      for j in range(i + 1, n):
        g[i][j] = int(s[i] != s[j])
        if i + 1 < j:
          g[i][j] += g[i + 1][j - 1]

    f = [[0] * (k + 1) for _ in range(n + 1)]
    for i in range(1, n + 1):
      for j in range(1, min(i, k) + 1):
        if j == 1:
          f[i][j] = g[0][i - 1]
        else:
          f[i][j] = inf
          for h in range(j - 1, i):
            f[i][j] = min(f[i][j], f[h][j - 1] + g[h][i - 1])
    return f[n][k]
class Solution {
  public int palindromePartition(String s, int k) {
    int n = s.length();
    int[][] g = new int[n][n];
    for (int i = n - 1; i >= 0; --i) {
      for (int j = i; j < n; ++j) {
        g[i][j] = s.charAt(i) != s.charAt(j) ? 1 : 0;
        if (i + 1 < j) {
          g[i][j] += g[i + 1][j - 1];
        }
      }
    }
    int[][] f = new int[n + 1][k + 1];
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= Math.min(i, k); ++j) {
        if (j == 1) {
          f[i][j] = g[0][i - 1];
        } else {
          f[i][j] = 10000;
          for (int h = j - 1; h < i; ++h) {
            f[i][j] = Math.min(f[i][j], f[h][j - 1] + g[h][i - 1]);
          }
        }
      }
    }
    return f[n][k];
  }
}
class Solution {
public:
  int palindromePartition(string s, int k) {
    int n = s.size();
    vector<vector<int>> g(n, vector<int>(n));
    for (int i = n - 1; i >= 0; --i) {
      for (int j = i; j < n; ++j) {
        g[i][j] = s[i] != s[j] ? 1 : 0;
        if (i + 1 < j) g[i][j] += g[i + 1][j - 1];
      }
    }
    vector<vector<int>> f(n + 1, vector<int>(k + 1));
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= min(i, k); ++j) {
        if (j == 1) {
          f[i][j] = g[0][i - 1];
        } else {
          f[i][j] = 10000;
          for (int h = j - 1; h < i; ++h) {
            f[i][j] = min(f[i][j], f[h][j - 1] + g[h][i - 1]);
          }
        }
      }
    }
    return f[n][k];
  }
};
func palindromePartition(s string, k int) int {
  n := len(s)
  g := make([][]int, n)
  for i := range g {
    g[i] = make([]int, n)
  }
  for i := n - 1; i >= 0; i-- {
    for j := 1; j < n; j++ {
      if s[i] != s[j] {
        g[i][j] = 1
      }
      if i+1 < j {
        g[i][j] += g[i+1][j-1]
      }
    }
  }
  f := make([][]int, n+1)
  for i := range f {
    f[i] = make([]int, k+1)
  }
  for i := 1; i <= n; i++ {
    for j := 1; j <= min(i, k); j++ {
      if j == 1 {
        f[i][j] = g[0][i-1]
      } else {
        f[i][j] = 100000
        for h := j - 1; h < i; h++ {
          f[i][j] = min(f[i][j], f[h][j-1]+g[h][i-1])
        }
      }
    }
  }
  return f[n][k]
}

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

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

发布评论

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