Problem Description
Given a string s, return true if it is possible to split s into three non-empty palindromic substrings. A substring is a palindrome if it reads the same forwards and backwards.
Key Insights
- We need to split the string into exactly three parts where each part is a palindrome.
- Precomputing palindromic substrings using dynamic programming allows us to check any substring in constant time after an O(n²) preprocessing.
- Iterate over possible split points for the first and second cut while checking the precomputed DP table to verify each substring is a palindrome.
Space and Time Complexity
Time Complexity: O(n²) due to the DP precomputation of all palindromic substrings. Space Complexity: O(n²) because of the DP table used to store palindrome status for each substring.
Solution
We use a dynamic programming approach to precompute a 2D boolean table dp where dp[i][j] is true if the substring s[i...j] is a palindrome. To fill this table:
- Every single character is a palindrome.
- For substrings of length 2 or more, a substring is a palindrome if its endpoints match and the inner substring is a palindrome. Once the table is built, we iterate through potential split indices i and j (with 0 < i < j < n) to ensure: • The substring s[0...i-1] is a palindrome. • The substring s[i...j-1] is a palindrome. • The substring s[j...n-1] is a palindrome. The existence of such a split implies a valid partition, and we return true if found.