Skip to content

Commit 011c577

Browse files
committed
feat: add solution 1745. Palindrome Partitioning IV
1 parent 5734932 commit 011c577

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [1745. Palindrome Partitioning IV](https://leetcode.com/problems/palindrome-partitioning-iv)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>Given a string <code>s</code>, return <code>true</code> <em>if it is possible to split the string</em> <code>s</code> <em>into three <strong>non-empty</strong> palindromic substrings. Otherwise, return </em><code>false</code>.​​​​​</p>
6+
7+
<p>A string is said to be palindrome if it the same string when reversed.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre><strong>Input:</strong> s = "abcbdd"
13+
<strong>Output:</strong> true
14+
<strong>Explanation: </strong>"abcbdd" = "a" + "bcb" + "dd", and all three substrings are palindromes.
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre><strong>Input:</strong> s = "bcbddxy"
20+
<strong>Output:</strong> false
21+
<strong>Explanation: </strong>s cannot be split into 3 palindromes.
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>3 &lt;= s.length &lt;= 2000</code></li>
29+
<li><code>s</code>​​​​​​ consists only of lowercase English letters.</li>
30+
</ul>
31+
</div>
32+
33+
<p>&nbsp;</p>
34+
35+
## Solutions
36+
37+
**Solution: `Dynamic Programming`**
38+
39+
- Time complexity: <em>O(n<sup>2</sup>)</em>
40+
- Space complexity: <em>O(n<sup>2</sup>)</em>
41+
42+
<p>&nbsp;</p>
43+
44+
### **JavaScript**
45+
46+
```js
47+
/**
48+
* @param {string} s
49+
* @return {boolean}
50+
*/
51+
const checkPartitioning = function (s) {
52+
const n = s.length;
53+
const dp = Array.from({ length: n }, () => new Array(n).fill(null));
54+
55+
const isPalindrome = (a, b) => {
56+
if (a >= b) return true;
57+
if (dp[a][b] !== null) return dp[a][b];
58+
const result = s[a] === s[b] && isPalindrome(a + 1, b - 1);
59+
60+
dp[a][b] = result;
61+
62+
return result;
63+
};
64+
65+
for (let a = 0; a < n - 2; a++) {
66+
const segment1 = isPalindrome(0, a);
67+
68+
if (!segment1) continue;
69+
70+
for (let b = a + 1; b < n - 1; b++) {
71+
const segment2 = isPalindrome(a + 1, b);
72+
73+
if (!segment2) continue;
74+
const segment3 = isPalindrome(b + 1, n - 1);
75+
76+
if (segment3) return true;
77+
}
78+
}
79+
80+
return false;
81+
};
82+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
const checkPartitioning = function (s) {
6+
const n = s.length;
7+
const dp = Array.from({ length: n }, () => new Array(n).fill(null));
8+
9+
const isPalindrome = (a, b) => {
10+
if (a >= b) return true;
11+
if (dp[a][b] !== null) return dp[a][b];
12+
const result = s[a] === s[b] && isPalindrome(a + 1, b - 1);
13+
14+
dp[a][b] = result;
15+
16+
return result;
17+
};
18+
19+
for (let a = 0; a < n - 2; a++) {
20+
const segment1 = isPalindrome(0, a);
21+
22+
if (!segment1) continue;
23+
24+
for (let b = a + 1; b < n - 1; b++) {
25+
const segment2 = isPalindrome(a + 1, b);
26+
27+
if (!segment2) continue;
28+
const segment3 = isPalindrome(b + 1, n - 1);
29+
30+
if (segment3) return true;
31+
}
32+
}
33+
34+
return false;
35+
};

0 commit comments

Comments
 (0)