# Palindrome Partitioning

Posted: 23 Oct, 2020

Difficulty: Moderate

#### You are given a string 'S'. Your task is to partition 'S' such that every substring of the partition is a palindrome. You need to return all possible palindrome partitioning of 'S'.

#### Note: A substring is a contiguous segment of a string.

##### For Example:

```
For a given string “BaaB”
3 possible palindrome partitioning of the given string are:
{“B”, “a”, “a”, “B”}
{“B”, “aa”, “B”}
{“BaaB”}
Every substring of all the above partitions of “BaaB” is a palindrome.
```

##### Input Format:

```
The only line of input contains a string 'S'.
```

##### Output Format :

```
For each test case, print all the possible palindromic partitions of the given string in a separate line.
Each substring of a partition is written within quotes(““) and separated by comma(,) and space, and each partition of the given string is written inside square brackets[].
The output of each test case will be printed in a separate line.
```

##### Note:

```
All the substrings of a partition are sorted in lexicographical order in the output. You just need to return the partitions in any order.
You do not need to print or sort anything, it has already been taken care of. Just implement the function.
```

##### Constraints :

```
0 <= |S|<= 15
where |S| denotes the length of string 'S'.
Time Limit: 1 sec.
```

Approach 1

- We are iterating each time to check whether a given substring is palindrome or not and there we are iterating the same substring of string again and again. So there are overlapping subproblems
**.** - So we use dynamic programming. To overcome the overlapping subproblems we can initially create the
**DP**table which stores if substring[i….j] is palindrome or not. We maintain a boolean**dp[N][N]**that is filled in a bottom-up manner. The value of**dp[i][j]**is true if the substring is a palindrome, otherwise false. - To calculate
**dp[i][j]**, we first check the value of**dp[i+1][j-1]**, if the value is true and**S[i]**is the same as**S[j]**, then we make**dp[i][j]**true. Otherwise, the value of**dp[i][j]**is made false. - Now to check if the substring is palindrome or not we only check if
**dp[start][k]**is true or not in the recursion. And the rest of the algorithm is the same as the previous approach to generate all possible substrings.

SIMILAR PROBLEMS

# Reverse String

Posted: 15 Apr, 2022

Difficulty: Moderate

# Ninja And The Clan

Posted: 17 Apr, 2022

Difficulty: Moderate

# Prime Digit Sum

Posted: 17 Apr, 2022

Difficulty: Hard

# Count Numbers Containing Given Digit K Times

Posted: 20 Apr, 2022

Difficulty: Moderate

# Count Numbers With Equal First and Last Digit

Posted: 20 Apr, 2022

Difficulty: Moderate