diff --git a/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/README.md b/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/README.md index 920353b5c..b0f5f9e10 100755 --- a/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/README.md +++ b/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/Solution.go b/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/Solution.go index d115ccf5e..7ff90e66f 100644 --- a/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/Solution.go +++ b/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/Solution.go @@ -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 } diff --git a/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/Solution_test.go b/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/Solution_test.go index 14ff50eb4..8bb0d5269 100644 --- a/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/Solution_test.go +++ b/leetcode/1701-1800/1745.Palindrome-Partitioning-IV/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }