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: s = "abcbdd" Output: true Explanation: "abcbdd" = "a" + "bcb" + "dd", and all three substrings are palindromes.
Example 2:
Input: s = "bcbddxy" Output: false Explanation: s cannot be split into 3 palindromes.
Constraints:
3 <= s.length <= 2000s consists only of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks whether a given string can be divided into three non-empty palindromic substrings. We can solve this by precomputing the palindrome status for every possible substring using dynamic programming. After building the table, we iterate through all possible split locations to check if the three resulting parts are all palindromes.
Here's how the algorithm would work step-by-step:
def check_partitioning(target_string):
string_length = len(target_string)
# Initialize a 2D boolean table to store whether substrings are palindromes.
is_palindrome = [[False] * string_length for _ in range(string_length)]
# Populate the palindrome table for all substring lengths.
for end_index in range(string_length):
for start_index in range(end_index + 1):
if target_string[start_index] == target_string[end_index]:
if end_index - start_index <= 2 or is_palindrome[start_index + 1][end_index - 1]:
is_palindrome[start_index][end_index] = True
# Check all possible split points into three non-empty substrings.
for first_cut in range(string_length - 2):
if is_palindrome[0][first_cut]:
for second_cut in range(first_cut + 1, string_length - 1):
if is_palindrome[first_cut + 1][second_cut] and is_palindrome[second_cut + 1][string_length - 1]:
return True
return False
To figure out if a word can be chopped into three smaller palindromes, we can precompute which parts of the word are already palindromes. Then, we systematically test every possible way to make two cut marks to see if all three resulting pieces read the same forwards and backwards.
Here's how the algorithm would work step-by-step:
class Solution:
def checkPartitioning(self, input_string: str) -> bool:
string_length = len(input_string)
palindrome_table = [[False] * string_length for _ in range(string_length)]
# Precompute all palindrome substrings to ensure efficient validation of each piece in constant time.
for substring_length in range(1, string_length + 1):
for start_index in range(string_length - substring_length + 1):
end_index = start_index + substring_length - 1
if input_string[start_index] == input_string[end_index]:
if substring_length <= 2 or palindrome_table[start_index + 1][end_index - 1]:
palindrome_table[start_index][end_index] = True
# Iterate through all possible cut pairs to test every possible way to divide into three parts.
for first_cut in range(string_length - 2):
if palindrome_table[0][first_cut]:
for second_cut in range(first_cut + 1, string_length - 1):
if palindrome_table[first_cut + 1][second_cut] and palindrome_table[second_cut + 1][string_length - 1]:
# Return true immediately once a valid three-part palindrome partition is discovered.
return True
return False| Case | How to Handle |
|---|---|
| String with length less than 3 | Since we need to partition the string into three non-empty substrings, any string of length 1 or 2 should immediately return false. |
| String of exactly length 3 with distinct characters | The algorithm will correctly check if each single-character substring is a palindrome, returning true since single characters are always palindromes. |
| String with all identical characters | Dynamic programming state transitions will easily identify valid palindrome cuts at any arbitrary index boundaries. |
| Maximum length constraints up to 2000 characters | A naive backtracking approach would TLE, so we must precompute a 2D boolean palindrome table in O(N^2) time to scale efficiently. |
| No valid partition into three palindromes exists | The nested loops will exhaust all possible cut combinations and naturally return false after failing to find a valid split. |
| Multiple overlapping valid palindrome partitions | The algorithm only needs to find at least one valid combination, so returning true upon the first successful triplet match avoids redundant checks. |
| Memory limit constraints with large 2D boolean arrays | Using a flat 1D array or optimizing the boolean grid storage prevents memory overhead issues in strict environments. |