Skip to content

Commit 7bdd8c2

Browse files
authored
feat: update solutions to lc problems: No.0131,0132 (#4120)
1 parent fc4feaf commit 7bdd8c2

File tree

6 files changed

+35
-23
lines changed

6 files changed

+35
-23
lines changed

solution/0100-0199/0131.Palindrome Partitioning/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func partition(s string) (ans [][]string) {
210210
```ts
211211
function partition(s: string): string[][] {
212212
const n = s.length;
213-
const f: boolean[][] = new Array(n).fill(0).map(() => new Array(n).fill(true));
213+
const f: boolean[][] = Array.from({ length: n }, () => Array(n).fill(true));
214214
for (let i = n - 1; i >= 0; --i) {
215215
for (let j = i + 1; j < n; ++j) {
216216
f[i][j] = s[i] === s[j] && f[i + 1][j - 1];

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ tags:
4242

4343
<!-- solution:start -->
4444

45-
### Solution 1
45+
### Solution 1: Preprocessing + DFS (Backtracking)
46+
47+
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.
48+
49+
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$.
50+
51+
If $i = |s|$, it means the partitioning is complete, and we add $t$ to the answer array and then return.
52+
53+
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]$.
54+
55+
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.
4656

4757
<!-- tabs:start -->
4858

@@ -191,7 +201,7 @@ func partition(s string) (ans [][]string) {
191201
```ts
192202
function partition(s: string): string[][] {
193203
const n = s.length;
194-
const f: boolean[][] = new Array(n).fill(0).map(() => new Array(n).fill(true));
204+
const f: boolean[][] = Array.from({ length: n }, () => Array(n).fill(true));
195205
for (let i = n - 1; i >= 0; --i) {
196206
for (let j = i + 1; j < n; ++j) {
197207
f[i][j] = s[i] === s[j] && f[i + 1][j - 1];

solution/0100-0199/0131.Palindrome Partitioning/Solution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function partition(s: string): string[][] {
22
const n = s.length;
3-
const f: boolean[][] = new Array(n).fill(0).map(() => new Array(n).fill(true));
3+
const f: boolean[][] = Array.from({ length: n }, () => Array(n).fill(true));
44
for (let i = n - 1; i >= 0; --i) {
55
for (let j = i + 1; j < n; ++j) {
66
f[i][j] = s[i] === s[j] && f[i + 1][j - 1];

solution/0100-0199/0132.Palindrome Partitioning II/README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,13 @@ func minCut(s string) int {
198198
```ts
199199
function minCut(s: string): number {
200200
const n = s.length;
201-
const g: boolean[][] = Array(n)
202-
.fill(0)
203-
.map(() => Array(n).fill(true));
201+
const g: boolean[][] = Array.from({ length: n }, () => Array(n).fill(true));
204202
for (let i = n - 1; ~i; --i) {
205203
for (let j = i + 1; j < n; ++j) {
206204
g[i][j] = s[i] === s[j] && g[i + 1][j - 1];
207205
}
208206
}
209-
const f: number[] = Array(n)
210-
.fill(0)
211-
.map((_, i) => i);
207+
const f: number[] = Array.from({ length: n }, (_, i) => i);
212208
for (let i = 1; i < n; ++i) {
213209
for (let j = 0; j <= i; ++j) {
214210
if (g[j][i]) {

solution/0100-0199/0132.Palindrome Partitioning II/README_EN.md

+17-7
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,21 @@ tags:
5858

5959
<!-- solution:start -->
6060

61-
### Solution 1
61+
### Solution 1: Dynamic Programming
62+
63+
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.
64+
65+
Next, we define $f[i]$ to represent the minimum number of cuts needed for the substring $s[0..i-1]$. Initially, $f[i] = i$.
66+
67+
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:
68+
69+
$$
70+
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}
71+
$$
72+
73+
The answer is $f[n]$, where $n$ is the length of the string $s$.
74+
75+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the length of the string $s$.
6276

6377
<!-- tabs:start -->
6478

@@ -178,17 +192,13 @@ func minCut(s string) int {
178192
```ts
179193
function minCut(s: string): number {
180194
const n = s.length;
181-
const g: boolean[][] = Array(n)
182-
.fill(0)
183-
.map(() => Array(n).fill(true));
195+
const g: boolean[][] = Array.from({ length: n }, () => Array(n).fill(true));
184196
for (let i = n - 1; ~i; --i) {
185197
for (let j = i + 1; j < n; ++j) {
186198
g[i][j] = s[i] === s[j] && g[i + 1][j - 1];
187199
}
188200
}
189-
const f: number[] = Array(n)
190-
.fill(0)
191-
.map((_, i) => i);
201+
const f: number[] = Array.from({ length: n }, (_, i) => i);
192202
for (let i = 1; i < n; ++i) {
193203
for (let j = 0; j <= i; ++j) {
194204
if (g[j][i]) {

solution/0100-0199/0132.Palindrome Partitioning II/Solution.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
function minCut(s: string): number {
22
const n = s.length;
3-
const g: boolean[][] = Array(n)
4-
.fill(0)
5-
.map(() => Array(n).fill(true));
3+
const g: boolean[][] = Array.from({ length: n }, () => Array(n).fill(true));
64
for (let i = n - 1; ~i; --i) {
75
for (let j = i + 1; j < n; ++j) {
86
g[i][j] = s[i] === s[j] && g[i + 1][j - 1];
97
}
108
}
11-
const f: number[] = Array(n)
12-
.fill(0)
13-
.map((_, i) => i);
9+
const f: number[] = Array.from({ length: n }, (_, i) => i);
1410
for (let i = 1; i < n; ++i) {
1511
for (let j = 0; j <= i; ++j) {
1612
if (g[j][i]) {

0 commit comments

Comments
 (0)