class Solution:
def checkPartitioning(self, s: str) -> bool:
n = len(s)
# Precompute palindrome substrings
is_pal = [[False] * n for _ in range(n)]
for i in range(n):
is_pal[i][i] = True
for i in range(n-1):
is_pal[i][i+1] = (s[i] == s[i+1])
for length in range(3, n+1):
for i in range(n-length+1):
j = i + length - 1
is_pal[i][j] = (s[i] == s[j]) and is_pal[i+1][j-1]
# Try all possible two cuts
for i in range(1, n-1):
for j in range(i, n-1):
if is_pal[0][i-1] and is_pal[i][j] and is_pal[j+1][n-1]:
return True
return False
class Solution {
public:
bool checkPartitioning(string s) {
int n = s.size();
vector<vector<bool>> is_pal(n, vector<bool>(n, false));
for (int i = 0; i < n; ++i) is_pal[i][i] = true;
for (int i = 0; i + 1 < n; ++i) is_pal[i][i+1] = (s[i] == s[i+1]);
for (int len = 3; len <= n; ++len) {
for (int i = 0; i + len - 1 < n; ++i) {
int j = i + len - 1;
is_pal[i][j] = (s[i] == s[j]) && is_pal[i+1][j-1];
}
}
for (int i = 1; i < n-1; ++i) {
for (int j = i; j < n-1; ++j) {
if (is_pal[0][i-1] && is_pal[i][j] && is_pal[j+1][n-1])
return true;
}
}
return false;
}
};
class Solution {
public boolean checkPartitioning(String s) {
int n = s.length();
boolean[][] isPal = new boolean[n][n];
for (int i = 0; i < n; i++) isPal[i][i] = true;
for (int i = 0; i + 1 < n; i++) isPal[i][i+1] = (s.charAt(i) == s.charAt(i+1));
for (int len = 3; len <= n; len++) {
for (int i = 0; i + len - 1 < n; i++) {
int j = i + len - 1;
isPal[i][j] = (s.charAt(i) == s.charAt(j)) && isPal[i+1][j-1];
}
}
for (int i = 1; i < n-1; i++) {
for (int j = i; j < n-1; j++) {
if (isPal[0][i-1] && isPal[i][j] && isPal[j+1][n-1]) {
return true;
}
}
}
return false;
}
}
var checkPartitioning = function(s) {
const n = s.length;
const isPal = Array.from({length: n}, () => Array(n).fill(false));
for (let i = 0; i < n; ++i) isPal[i][i] = true;
for (let i = 0; i + 1 < n; ++i) isPal[i][i+1] = (s[i] === s[i+1]);
for (let len = 3; len <= n; ++len) {
for (let i = 0; i + len - 1 < n; ++i) {
let j = i + len - 1;
isPal[i][j] = (s[i] === s[j]) && isPal[i+1][j-1];
}
}
for (let i = 1; i < n-1; ++i) {
for (let j = i; j < n-1; ++j) {
if (isPal[0][i-1] && isPal[i][j] && isPal[j+1][n-1]) {
return true;
}
}
}
return false;
};
s
, determine if it is possible to split s
into exactly three non-empty substrings such that each substring is a palindrome. Each substring must be contiguous, and every character in s
must be used in exactly one substring. You must return true
if at least one such partition exists, and false
otherwise.
s
must be included in exactly one substring.is_pal[i][j]
that is true
if the substring s[i..j]
is a palindrome.s[i..j]
is a palindrome if s[i] == s[j]
and s[i+1..j-1]
is a palindrome.s
into three non-empty parts, so the first cut can be at any position i
(from 1 to n-2), and the second cut at any position j
(from i
to n-2).(i, j)
pair, check if s[0..i-1]
, s[i..j]
, and s[j+1..n-1]
are all palindromes using our precomputed array.true
.false
.s = "abcbdd"
.
"a"
, "b"
, "c"
, "b"
, "d"
, "d"
are all palindromes by default. "bcb"
is also a palindrome.i = 1
, second cut at j = 1
:
s[0..0] = "a"
(palindrome)s[1..1] = "b"
(palindrome)s[2..5] = "cbdd"
(not a palindrome)i = 1
, j = 2
:
s[0..0] = "a"
(palindrome)s[1..2] = "bc"
(not a palindrome)i = 1
, j = 3
:
s[0..0] = "a"
(palindrome)s[1..3] = "bcb"
(palindrome)s[4..5] = "dd"
(palindrome)true
.