- Related Questions & Answers
- Program to find number of ways we can decode a message in 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 count number of ways we can throw n dices in Python
- Program to count number of ways we can distribute coins to workers 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
- Program to find maximum number of coins we can collect in Python
- C++ Program to Find number of Ways to Partition a word such that each word is a Palindrome
- Program to count number of ways we can make a list of values by splitting numeric string in Python
- Program to check how many ways we can choose empty cells of a matrix in python
- Program to check number of ways we can move k times and return back to first place in python
- Program to find possible number of palindromes we can make by trimming string in Python
- Program to find maximum number of boxes we can fit inside another boxes in python

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

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:

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))

"xyyx"

3

Advertisements