LeetCode //C - 1278. Palindrome Partitioning III

1278. Palindrome Partitioning III

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.

From: LeetCode

Link: 1278. Palindrome Partitioning III


Solution:

Ideas:

Precompute the cost to convert every substring into a palindrome, then use dynamic programming to find the minimum cost of splitting the string into exactly k parts.

Code:
c 复制代码
#include <string.h>
#include <limits.h>

int palindromePartition(char* s, int k) {
    int n = strlen(s);

    // cost[i][j] = minimum changes needed to make s[i..j] a palindrome
    int cost[100][100] = {0};

    for (int length = 2; length <= n; length++) {
        for (int i = 0; i + length <= n; i++) {
            int j = i + length - 1;

            cost[i][j] = cost[i + 1][j - 1];

            if (s[i] != s[j]) {
                cost[i][j]++;
            }
        }
    }

    // dp[parts][i] = minimum changes to split first i characters
    // into exactly parts palindromic substrings
    int dp[101][101];

    for (int parts = 0; parts <= k; parts++) {
        for (int i = 0; i <= n; i++) {
            dp[parts][i] = INT_MAX / 2;
        }
    }

    dp[0][0] = 0;

    for (int parts = 1; parts <= k; parts++) {
        for (int i = parts; i <= n; i++) {
            for (int split = parts - 1; split < i; split++) {
                int current =
                    dp[parts - 1][split] + cost[split][i - 1];

                if (current < dp[parts][i]) {
                    dp[parts][i] = current;
                }
            }
        }
    }

    return dp[k][n];
}
相关推荐
春弦_20 小时前
自用算法整理二
数据结构·算法·图论
hetao173383721 小时前
2026-10-22~23 hetao1733837 的刷题记录
c++·算法
java_nnnn21 小时前
LeetCode刷题滑动窗口(串联所有单词的子串+最小覆盖的子串)Java
算法·leetcode·职场和发展
Navigator_Z1 天前
LeetCode //C - 1301. Number of Paths with Max Score
c语言·算法·leetcode
卡卡~~1 天前
“101计划”核心课程实践教材《数据实验结构指导-C++语言版》题目集
开发语言·c++·学习·算法
学逆向的1 天前
C 多文件开发
c语言·开发语言·网络安全
企业数字化笔记1 天前
AI视频增强后画质怎么验收?插值基线、伪细节与帧间闪烁的检查方法
人工智能·算法·音视频
学逆向的1 天前
C 内存属性
c语言·开发语言·算法·网络安全
Zwawa1 天前
一盏 LED 不够用了:让同一盏灯说出不同的话
c语言·单片机·嵌入式