Skip to content

Commit 6dde50c

Browse files
committed
Add solution and test-cases for problem 1745
1 parent a337ff8 commit 6dde50c

File tree

3 files changed

+67
-22
lines changed

3 files changed

+67
-22
lines changed

leetcode/1701-1800/1745.Palindrome-Partitioning-IV/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [1745.Palindrome Partitioning IV][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a string `s`, return `true` if it is possible to split the string `s` into three **non-empty** palindromic substrings. Otherwise, return `false`.
5+
6+
A string is said to be palindrome if it the same string when reversed.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: s = "abcbdd"
12+
Output: true
13+
Explanation: "abcbdd" = "a" + "bcb" + "dd", and all three substrings are palindromes.
1314
```
1415

15-
## 题意
16-
> ...
17-
18-
## 题解
16+
**Example 2:**
1917

20-
### 思路1
21-
> ...
22-
Palindrome Partitioning IV
23-
```go
2418
```
25-
19+
Input: s = "bcbddxy"
20+
Output: false
21+
Explanation: s cannot be split into 3 palindromes.
22+
```
2623

2724
## 结语
2825

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func isPalindrome(s string) bool {
4+
l, r := 0, len(s)-1
5+
for ; l < r; l, r = l+1, r-1 {
6+
if s[l] != s[r] {
7+
return false
8+
}
9+
}
10+
return true
11+
}
12+
13+
func Solution(s string) bool {
14+
l := len(s)
15+
if l == 3 {
16+
return true
17+
}
18+
left := make([]bool, l)
19+
left[0] = true
20+
for i := 1; i < l; i++ {
21+
left[i] = isPalindrome(s[:i+1])
22+
}
23+
right := make([]bool, l)
24+
right[l-1] = true
25+
for i := l - 2; i >= 0; i-- {
26+
right[i] = isPalindrome(s[i:])
27+
}
28+
29+
for i := 1; i < l-1; i++ {
30+
for ll, r := i-1, i+1; ll >= 0 && r <= l-1; ll, r = ll-1, r+1 {
31+
if left[ll] && right[r] {
32+
return true
33+
}
34+
if s[ll] != s[r] {
35+
break
36+
}
37+
}
38+
}
39+
for i := 1; i < l-2; i++ {
40+
if s[i] != s[i+1] {
41+
continue
42+
}
43+
44+
for ll, r := i-1, i+2; ll >= 0 && r <= l-1; ll, r = ll-1, r+1 {
45+
if left[ll] && right[r] {
46+
return true
47+
}
48+
if s[ll] != s[r] {
49+
break
50+
}
51+
}
52+
}
53+
return false
554
}

leetcode/1701-1800/1745.Palindrome-Partitioning-IV/Solution_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abcbdd", true},
17+
{"TestCase2", "bcbddxy", false},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)