Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check a string can be split into three palindromes or not in Python
Given a string, we need to check whether it can be split into exactly three palindromic substrings. A palindrome reads the same forwards and backwards.
So, if the input is like s = "levelpopracecar", then the output will be True because we can split it like "level", "pop", "racecar" − all are palindromes.
Algorithm
To solve this, we will follow these steps −
Create a 2D DP table to store which substrings are palindromes
Fill the DP table using dynamic programming
Try all possible ways to split the string into three parts
Check if all three parts are palindromes using the DP table
Example
Let us see the following implementation to get better understanding −
def solve(s):
n = len(s)
# Create DP table to store palindrome information
dp = [[False] * n for _ in range(n)]
# Fill DP table - check all substrings
for i in range(n-1, -1, -1):
for j in range(n):
if i >= j:
dp[i][j] = True # Single char or empty string
elif s[i] == s[j]:
dp[i][j] = dp[i+1][j-1] # Check inner substring
# Try all possible splits into 3 parts
for i in range(1, n):
for j in range(i+1, n):
# Check if all three parts are palindromes
if dp[0][i-1] and dp[i][j-1] and dp[j][n-1]:
return True
return False
# Test the function
s = "levelpopracecar"
result = solve(s)
print(f"Can '{s}' be split into 3 palindromes? {result}")
# Let's also show the actual split
def find_split(s):
n = len(s)
dp = [[False] * n for _ in range(n)]
for i in range(n-1, -1, -1):
for j in range(n):
if i >= j:
dp[i][j] = True
elif s[i] == s[j]:
dp[i][j] = dp[i+1][j-1]
for i in range(1, n):
for j in range(i+1, n):
if dp[0][i-1] and dp[i][j-1] and dp[j][n-1]:
return [s[0:i], s[i:j], s[j:n]]
return None
split_result = find_split(s)
if split_result:
print(f"Split: {split_result}")
Can 'levelpopracecar' be split into 3 palindromes? True Split: ['level', 'pop', 'racecar']
How It Works
The algorithm works in two phases −
Phase 1: Build a DP table where dp[i][j] indicates if substring from index i to j is a palindrome. We fill this table using the fact that a string is palindromic if its first and last characters match and the inner substring is also palindromic.
Phase 2: Try all possible ways to split the string into three parts. For each split at positions i and j, check if all three substrings s[0:i], s[i:j], and s[j:n] are palindromes using our DP table.
Time Complexity
The time complexity is O(n²) for building the DP table and O(n²) for checking all possible splits, giving us overall O(n²) complexity.
Conclusion
This dynamic programming approach efficiently determines if a string can be split into exactly three palindromes by pre-computing palindrome information and then checking all possible three-way splits.
