Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.1278 #4122

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 73 additions & 3 deletions solution/1200-1299/1278.Palindrome Partitioning III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ tags:

### 方法一:动态规划

定义 $dp[i][j]$ 表示将字符串 $s$ 的前 $i$ 个字符分割成 $j$ 个回文串所需要的最少修改次数,我们假定 $i$ 下标从 $1$ 开始,答案为 $dp[n][k]$。
我们定义 $f[i][j]$ 表示将字符串 $s$ 的前 $i$ 个字符分割成 $j$ 个回文串所需要的最少修改次数,我们假定 $i$ 下标从 $1$ 开始,答案为 $f[n][k]$。

对于 $dp[i][j]$,我们可以枚举第 $j-1$ 个回文串的最后一个字符的位置 $h$,那么 $dp[i][j]$ 就等于 $dp[h][j-1] + g[h][i-1]$ 的较小值,其中 $g[h][i-1]$ 表示将字符串 $s[h..i-1]$ 变成回文串所需要的最少修改次数(这一部分我们可以通过预处理得到,时间复杂度 $O(n^2)$。
对于 $f[i][j]$,我们可以枚举第 $j-1$ 个回文串的最后一个字符的位置 $h$,那么 $f[i][j]$ 就等于 $f[h][j-1] + g[h][i-1]$ 的较小值,其中 $g[h][i-1]$ 表示将字符串 $s[h..i-1]$ 变成回文串所需要的最少修改次数(这一部分我们可以通过预处理得到,时间复杂度 $O(n^2)$。

时间复杂度 $O(n^2\times k)$。其中 $n$ 为字符串 $s$ 的长度。
时间复杂度 $O(n^2 \times k)$,空间复杂度 $O(n \times (n + k))$。其中 $n$ 为字符串 $s$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -205,6 +205,76 @@ func palindromePartition(s string, k int) int {
}
```

#### TypeScript

```ts
function palindromePartition(s: string, k: number): number {
const n = s.length;
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
for (let i = n - 1; i >= 0; i--) {
for (let j = i + 1; j < n; j++) {
g[i][j] = s[i] !== s[j] ? 1 : 0;
if (i + 1 < j) {
g[i][j] += g[i + 1][j - 1];
}
}
}
const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0));
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= Math.min(i, k); j++) {
if (j === 1) {
f[i][j] = g[0][i - 1];
} else {
f[i][j] = 1 << 30;
for (let 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];
}
```

#### Rust

```rust
impl Solution {
pub fn palindrome_partition(s: String, k: i32) -> i32 {
let n = s.len();
let s: Vec<char> = s.chars().collect();
let mut g = vec![vec![0; n]; n];

for i in (0..n).rev() {
for j in i + 1..n {
g[i][j] = if s[i] != s[j] { 1 } else { 0 };
if i + 1 < j {
g[i][j] += g[i + 1][j - 1];
}
}
}

let mut f = vec![vec![0; (k + 1) as usize]; n + 1];
let inf = i32::MAX;

for i in 1..=n {
for j in 1..=i.min(k as usize) {
if j == 1 {
f[i][j] = g[0][i - 1];
} else {
f[i][j] = inf;
for h in (j - 1)..i {
f[i][j] = f[i][j].min(f[h][j - 1] + g[h][i - 1]);
}
}
}
}

f[n][k as usize]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
78 changes: 77 additions & 1 deletion solution/1200-1299/1278.Palindrome Partitioning III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

We define $f[i][j]$ to represent the minimum number of changes needed to partition the first $i$ characters of the string $s$ into $j$ palindromic substrings. We assume the index $i$ starts from 1, and the answer is $f[n][k]$.

For $f[i][j]$, we can enumerate the position $h$ of the last character of the $(j-1)$-th palindromic substring. Then $f[i][j]$ is equal to the minimum value of $f[h][j-1] + g[h][i-1]$, where $g[h][i-1]$ represents the minimum number of changes needed to turn the substring $s[h..i-1]$ into a palindrome (this part can be preprocessed with a time complexity of $O(n^2)$).

The time complexity is $O(n^2 \times k)$, and the space complexity is $O(n \times (n + k))$. Where $n$ is the length of the string $s$.

<!-- tabs:start -->

Expand Down Expand Up @@ -198,6 +204,76 @@ func palindromePartition(s string, k int) int {
}
```

#### TypeScript

```ts
function palindromePartition(s: string, k: number): number {
const n = s.length;
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
for (let i = n - 1; i >= 0; i--) {
for (let j = i + 1; j < n; j++) {
g[i][j] = s[i] !== s[j] ? 1 : 0;
if (i + 1 < j) {
g[i][j] += g[i + 1][j - 1];
}
}
}
const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0));
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= Math.min(i, k); j++) {
if (j === 1) {
f[i][j] = g[0][i - 1];
} else {
f[i][j] = 1 << 30;
for (let 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];
}
```

#### Rust

```rust
impl Solution {
pub fn palindrome_partition(s: String, k: i32) -> i32 {
let n = s.len();
let s: Vec<char> = s.chars().collect();
let mut g = vec![vec![0; n]; n];

for i in (0..n).rev() {
for j in i + 1..n {
g[i][j] = if s[i] != s[j] { 1 } else { 0 };
if i + 1 < j {
g[i][j] += g[i + 1][j - 1];
}
}
}

let mut f = vec![vec![0; (k + 1) as usize]; n + 1];
let inf = i32::MAX;

for i in 1..=n {
for j in 1..=i.min(k as usize) {
if j == 1 {
f[i][j] = g[0][i - 1];
} else {
f[i][j] = inf;
for h in (j - 1)..i {
f[i][j] = f[i][j].min(f[h][j - 1] + g[h][i - 1]);
}
}
}
}

f[n][k as usize]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
34 changes: 34 additions & 0 deletions solution/1200-1299/1278.Palindrome Partitioning III/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
impl Solution {
pub fn palindrome_partition(s: String, k: i32) -> i32 {
let n = s.len();
let s: Vec<char> = s.chars().collect();
let mut g = vec![vec![0; n]; n];

for i in (0..n).rev() {
for j in i + 1..n {
g[i][j] = if s[i] != s[j] { 1 } else { 0 };
if i + 1 < j {
g[i][j] += g[i + 1][j - 1];
}
}
}

let mut f = vec![vec![0; (k + 1) as usize]; n + 1];
let inf = i32::MAX;

for i in 1..=n {
for j in 1..=i.min(k as usize) {
if j == 1 {
f[i][j] = g[0][i - 1];
} else {
f[i][j] = inf;
for h in (j - 1)..i {
f[i][j] = f[i][j].min(f[h][j - 1] + g[h][i - 1]);
}
}
}
}

f[n][k as usize]
}
}
26 changes: 26 additions & 0 deletions solution/1200-1299/1278.Palindrome Partitioning III/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function palindromePartition(s: string, k: number): number {
const n = s.length;
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
for (let i = n - 1; i >= 0; i--) {
for (let j = i + 1; j < n; j++) {
g[i][j] = s[i] !== s[j] ? 1 : 0;
if (i + 1 < j) {
g[i][j] += g[i + 1][j - 1];
}
}
}
const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0));
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= Math.min(i, k); j++) {
if (j === 1) {
f[i][j] = g[0][i - 1];
} else {
f[i][j] = 1 << 30;
for (let 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];
}