- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of ways we can split a palindrome in python

Suppose we have a string s, we have to find the number of ways we can partition the string such that each part is a palindrome.

So, if the input is like s = "xyyx", then the output will be 3, as we have splits like: ["x", "yy", "x"], ["x", "y", "y", "x"], ["xyyx"].

To solve this, we will follow these steps:

- n := size of s
- table := a list of size n + 1 and fill it with 0
- table[0] := 1
- for i in range 0 to n, do
- for j in range 0 to i - 1, do
- sub := s[from index j to i]
- if sub is palindrome, then
- table[i] := table[i] + table[j]

- for j in range 0 to i - 1, do
- return last element of table

Let us see the following implementation to get better understanding:

## Example

class Solution: def solve(self, s): n = len(s) table = [1] + [0] * n for i in range(n + 1): for j in range(i): sub = s[j:i] if sub == sub[::-1]: table[i] += table[j] return table[-1] ob = Solution() s = "xyyx" print(ob.solve(s))

## Input

"xyyx"

## Output

3

- Related Questions & Answers
- Program to find number of ways to split a string in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of good ways to split a string using Python
- Program to find number of ways we can arrange symbols to get target in Python?
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find number of ways to split array into three subarrays in Python
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Program to find number of ways we can get n R.s using Indian denominations in Python
- Program to count number of ways we can throw n dices in Python
- Program to find number of ways we can reach to the next floor using stairs in Python
- Program to count number of ways we can distribute coins to workers in Python
- Program to find number of ways we can get a number which is sum of nth power of unique numbers in Python
- Program to find how many ways we can climb stairs in Python
- In how many ways can we split a string in JavaScript?
- Program to find number of ways we can reach from top left point to bottom right point in Python

Advertisements