Skip to content

Commit

Permalink
Merge pull request #1084 from 0xff-dev/1745
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1745
  • Loading branch information
6boris authored Jan 9, 2025
2 parents 183d2c1 + 6dde50c commit 02fd03c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
25 changes: 11 additions & 14 deletions leetcode/1701-1800/1745.Palindrome-Partitioning-IV/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
# [1745.Palindrome Partitioning IV][title]

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

A string is said to be palindrome if it the same string when reversed.

**Example 1:**

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

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Palindrome Partitioning IV
```go
```

Input: s = "bcbddxy"
Output: false
Explanation: s cannot be split into 3 palindromes.
```

## 结语

Expand Down
53 changes: 51 additions & 2 deletions leetcode/1701-1800/1745.Palindrome-Partitioning-IV/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
package Solution

func Solution(x bool) bool {
return x
func isPalindrome(s string) bool {
l, r := 0, len(s)-1
for ; l < r; l, r = l+1, r-1 {
if s[l] != s[r] {
return false
}
}
return true
}

func Solution(s string) bool {
l := len(s)
if l == 3 {
return true
}
left := make([]bool, l)
left[0] = true
for i := 1; i < l; i++ {
left[i] = isPalindrome(s[:i+1])
}
right := make([]bool, l)
right[l-1] = true
for i := l - 2; i >= 0; i-- {
right[i] = isPalindrome(s[i:])
}

for i := 1; i < l-1; i++ {
for ll, r := i-1, i+1; ll >= 0 && r <= l-1; ll, r = ll-1, r+1 {
if left[ll] && right[r] {
return true
}
if s[ll] != s[r] {
break
}
}
}
for i := 1; i < l-2; i++ {
if s[i] != s[i+1] {
continue
}

for ll, r := i-1, i+2; ll >= 0 && r <= l-1; ll, r = ll-1, r+1 {
if left[ll] && right[r] {
return true
}
if s[ll] != s[r] {
break
}
}
}
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
inputs string
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "abcbdd", true},
{"TestCase2", "bcbddxy", false},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 02fd03c

Please sign in to comment.