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

Add solution and test-cases for problem 1745 #1084

Merged
merged 1 commit into from
Jan 9, 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
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() {
}
Loading