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: update solutions to lc problems: No.0131,0132 #4120

Merged
merged 1 commit into from
Feb 28, 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
2 changes: 1 addition & 1 deletion solution/0100-0199/0131.Palindrome Partitioning/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func partition(s string) (ans [][]string) {
```ts
function partition(s: string): string[][] {
const n = s.length;
const f: boolean[][] = new Array(n).fill(0).map(() => new Array(n).fill(true));
const f: boolean[][] = Array.from({ length: n }, () => 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];
Expand Down
14 changes: 12 additions & 2 deletions solution/0100-0199/0131.Palindrome Partitioning/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Preprocessing + DFS (Backtracking)

We can use dynamic programming to preprocess whether any substring in the string is a palindrome, i.e., $f[i][j]$ indicates whether the substring $s[i..j]$ is a palindrome.

Next, we design a function $dfs(i)$, which represents starting from the $i$-th character of the string and partitioning it into several palindromic substrings, with the current partition scheme being $t$.

If $i = |s|$, it means the partitioning is complete, and we add $t$ to the answer array and then return.

Otherwise, we can start from $i$ and enumerate the end position $j$ from small to large. If $s[i..j]$ is a palindrome, we add $s[i..j]$ to $t$, then continue to recursively call $dfs(j+1)$. When backtracking, we need to pop $s[i..j]$.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -191,7 +201,7 @@ func partition(s string) (ans [][]string) {
```ts
function partition(s: string): string[][] {
const n = s.length;
const f: boolean[][] = new Array(n).fill(0).map(() => new Array(n).fill(true));
const f: boolean[][] = Array.from({ length: n }, () => 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];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function partition(s: string): string[][] {
const n = s.length;
const f: boolean[][] = new Array(n).fill(0).map(() => new Array(n).fill(true));
const f: boolean[][] = Array.from({ length: n }, () => 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];
Expand Down
8 changes: 2 additions & 6 deletions solution/0100-0199/0132.Palindrome Partitioning II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,13 @@ func minCut(s string) int {
```ts
function minCut(s: string): number {
const n = s.length;
const g: boolean[][] = Array(n)
.fill(0)
.map(() => Array(n).fill(true));
const g: boolean[][] = Array.from({ length: n }, () => Array(n).fill(true));
for (let i = n - 1; ~i; --i) {
for (let j = i + 1; j < n; ++j) {
g[i][j] = s[i] === s[j] && g[i + 1][j - 1];
}
}
const f: number[] = Array(n)
.fill(0)
.map((_, i) => i);
const f: number[] = Array.from({ length: n }, (_, i) => i);
for (let i = 1; i < n; ++i) {
for (let j = 0; j <= i; ++j) {
if (g[j][i]) {
Expand Down
24 changes: 17 additions & 7 deletions solution/0100-0199/0132.Palindrome Partitioning II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,21 @@ tags:

<!-- solution:start -->

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

First, we preprocess the string $s$ to determine whether each substring $s[i..j]$ is a palindrome, and record this in a 2D array $g[i][j]$, where $g[i][j]$ indicates whether the substring $s[i..j]$ is a palindrome.

Next, we define $f[i]$ to represent the minimum number of cuts needed for the substring $s[0..i-1]$. Initially, $f[i] = i$.

Next, we consider how to transition the state for $f[i]$. We can enumerate the previous cut point $j$. If the substring $s[j..i]$ is a palindrome, then $f[i]$ can be transitioned from $f[j]$. If $j = 0$, it means that $s[0..i]$ itself is a palindrome, and no cuts are needed, i.e., $f[i] = 0$. Therefore, the state transition equation is as follows:

$$
f[i] = \min_{0 \leq j \leq i} \begin{cases} f[j-1] + 1, & \textit{if}\ g[j][i] = \textit{True} \\ 0, & \textit{if}\ g[0][i] = \textit{True} \end{cases}
$$

The answer is $f[n]$, where $n$ is the length of the string $s$.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the length of the string $s$.

<!-- tabs:start -->

Expand Down Expand Up @@ -178,17 +192,13 @@ func minCut(s string) int {
```ts
function minCut(s: string): number {
const n = s.length;
const g: boolean[][] = Array(n)
.fill(0)
.map(() => Array(n).fill(true));
const g: boolean[][] = Array.from({ length: n }, () => Array(n).fill(true));
for (let i = n - 1; ~i; --i) {
for (let j = i + 1; j < n; ++j) {
g[i][j] = s[i] === s[j] && g[i + 1][j - 1];
}
}
const f: number[] = Array(n)
.fill(0)
.map((_, i) => i);
const f: number[] = Array.from({ length: n }, (_, i) => i);
for (let i = 1; i < n; ++i) {
for (let j = 0; j <= i; ++j) {
if (g[j][i]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
function minCut(s: string): number {
const n = s.length;
const g: boolean[][] = Array(n)
.fill(0)
.map(() => Array(n).fill(true));
const g: boolean[][] = Array.from({ length: n }, () => Array(n).fill(true));
for (let i = n - 1; ~i; --i) {
for (let j = i + 1; j < n; ++j) {
g[i][j] = s[i] === s[j] && g[i + 1][j - 1];
}
}
const f: number[] = Array(n)
.fill(0)
.map((_, i) => i);
const f: number[] = Array.from({ length: n }, (_, i) => i);
for (let i = 1; i < n; ++i) {
for (let j = 0; j <= i; ++j) {
if (g[j][i]) {
Expand Down
Loading