# Given a string, print all possible palindromic partitions

Given a string, find all possible palindromic partitions of given string.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Note that this problem is different from Palindrome Partitioning Problem, there the task was to find the partitioning with minimum cuts in input string. Here we need to print all possible partitions.

The idea is to go through every substring starting from first character, check if it is palindrome. If yes, then add the substring to solution and recur for remaining part. Below is complete algorithm.

Below is the implementation of above idea.

## C++

 `// C++ program to print all palindromic partitions of a given string ` `#include ` `using` `namespace` `std; ` ` `  `// A utility function to check if str is palindroem ` `bool` `isPalindrome(string str, ``int` `low, ``int` `high) ` `{ ` `    ``while` `(low < high) ` `    ``{ ` `        ``if` `(str[low] != str[high]) ` `            ``return` `false``; ` `        ``low++; ` `        ``high--; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Recursive function to find all palindromic partitions of str[start..n-1] ` `// allPart --> A vector of vector of strings. Every vector inside it stores ` `//             a partition ` `// currPart --> A vector of strings to store current partition  ` `void` `allPalPartUtil(vector >&allPart, vector &currPart,  ` `                   ``int` `start, ``int` `n, string str) ` `{ ` `    ``// If 'start' has reached len ` `    ``if` `(start >= n) ` `    ``{ ` `        ``allPart.push_back(currPart); ` `        ``return``; ` `    ``} ` ` `  `    ``// Pick all possible ending points for substrings ` `    ``for` `(``int` `i=start; i > allPart; ` ` `  `    ``// To store current palindromic partition ` `    ``vector currPart;  ` ` `  `    ``// Call recursive function to generate all partiions ` `    ``// and store in allPart ` `    ``allPalPartUtil(allPart, currPart, 0, n, str); ` ` `  `    ``// Print all partitions generated by above call ` `    ``for` `(``int` `i=0; i< allPart.size(); i++ ) ` `    ``{ ` `        ``for` `(``int` `j=0; j

## Java

 `// Java program to print all palindromic ` `// partitions of a given string  ` `import` `java.util.ArrayList; ` `import` `java.util.Deque; ` `import` `java.util.LinkedList; ` ` `  `public` `class` `PrintAllPalindrome ` `{ ` `    ``// Driver program ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``String input = ``"nitin"``; ` ` `  `        ``System.out.println(``"All possible palindrome"` `+  ` `                            ``"partions for "` `+ input  ` `                            ``+ ``" are :"``); ` ` `  `        ``allPalPartitions(input); ` `    ``} ` ` `  `    ``// Function to print all possible ` `    ``// palindromic partitions of str. ` `    ``// It mainly creates vectors and ` `    ``// calls allPalPartUtil() ` `    ``private` `static` `void` `allPalPartitions(String input)  ` `    ``{ ` `        ``int` `n = input.length(); ` ` `  `        ``// To Store all palindromic partitions ` `        ``ArrayList> allPart = ``new` `ArrayList<>(); ` ` `  `        ``// To store current palindromic partition ` `        ``Deque currPart = ``new` `LinkedList(); ` ` `  `        ``// Call recursive function to generate  ` `        ``// all partiions and store in allPart ` `        ``allPalPartitonsUtil(allPart, currPart, ``0``, n, input); ` ` `  `        ``// Print all partitions generated by above call ` `        ``for` `(``int` `i = ``0``; i < allPart.size(); i++)  ` `        ``{ ` `            ``for` `(``int` `j = ``0``; j < allPart.get(i).size(); j++) ` `            ``{ ` `                ``System.out.print(allPart.get(i).get(j) + ``" "``); ` `            ``} ` `            ``System.out.println(); ` `        ``} ` ` `  `    ``} ` ` `  `    ``// Recursive function to find all palindromic ` `    ``// partitions of input[start..n-1] allPart --> A ` `    ``// ArrayList of Deque of strings. Every Deque ` `    ``// inside it stores a partition currPart --> A ` `    ``// Deque of strings to store current partition ` `    ``private` `static` `void` `allPalPartitonsUtil(ArrayList> allPart,  ` `            ``Deque currPart, ``int` `start, ``int` `n, String input) ` `    ``{ ` `        ``// If 'start' has reached len ` `        ``if` `(start >= n)  ` `        ``{ ` `            ``allPart.add(``new` `ArrayList<>(currPart)); ` `            ``return``; ` `        ``} ` ` `  `        ``// Pick all possible ending points for substrings ` `        ``for` `(``int` `i = start; i < n; i++)  ` `        ``{ ` `             `  `            ``// If substring str[start..i] is palindrome ` `            ``if` `(isPalindrome(input, start, i)) ` `            ``{ ` `                 `  `                ``// Add the substring to result ` `                ``currPart.addLast(input.substring(start, i + ``1``)); ` ` `  `                ``// Recur for remaining remaining substring ` `                ``allPalPartitonsUtil(allPart, currPart, i + ``1``, n, input); ` ` `  `                ``// Remove substring str[start..i] from current ` `                ``// partition ` `                ``currPart.removeLast(); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// A utility function to check  ` `    ``// if input is Palindrome ` `    ``private` `static` `boolean` `isPalindrome(String input,  ` `                                    ``int` `start, ``int` `i) ` `    ``{ ` `        ``while` `(start < i)  ` `        ``{ ` `            ``if` `(input.charAt(start++) != input.charAt(i--)) ` `                ``return` `false``; ` `        ``} ` `        ``return` `true``; ` `    ``} ` `} ` ` `  `// This code is contributed by Prerna Saini `

## Python3

 `# Python3 program to print all  ` `# palindromic partitions of a given string ` ` `  `# A utility function to check if  ` `# str is palindrome ` `def` `isPalindrome(string: ``str``, ` `                 ``low: ``int``, high: ``int``): ` `    ``while` `low < high: ` `        ``if` `string[low] !``=` `string[high]: ` `            ``return` `False` `        ``low ``+``=` `1` `        ``high ``-``=` `1` `    ``return` `True` ` `  `# Recursive function to find all  ` `# palindromic partitions of str[start..n-1] ` `# allPart --> A vector of vector of strings.  ` `#             Every vector inside it stores a partition ` `# currPart --> A vector of strings to store current partition ` `def` `allPalPartUtil(allPart: ``list``, currPart: ``list``,  ` `                   ``start: ``int``, n: ``int``, string: ``str``): ` ` `  `    ``# If 'start' has reached len ` `    ``if` `start >``=` `n: ` `         `  `        ``# In Python list are passed by reference ` `        ``# that is why it is needed to copy first ` `        ``# and then append ` `        ``x ``=` `currPart.copy() ` ` `  `        ``allPart.append(x) ` `        ``return` ` `  `    ``# Pick all possible ending points for substrings ` `    ``for` `i ``in` `range``(start, n): ` ` `  `        ``# If substring str[start..i] is palindrome ` `        ``if` `isPalindrome(string, start, i): ` ` `  `            ``# Add the substring to result ` `            ``currPart.append(string[start:i ``+` `1``]) ` ` `  `            ``# Recur for remaining remaining substring ` `            ``allPalPartUtil(allPart, currPart,  ` `                            ``i ``+` `1``, n, string) ` ` `  `            ``# Remove substring str[start..i]  ` `            ``# from current partition ` `            ``currPart.pop() ` ` `  `# Function to print all possible  ` `# palindromic partitions of str.  ` `# It mainly creates vectors and  ` `# calls allPalPartUtil() ` `def` `allPalPartitions(string: ``str``): ` ` `  `    ``n ``=` `len``(string) ` ` `  `    ``# To Store all palindromic partitions ` `    ``allPart ``=` `[] ` ` `  `    ``# To store current palindromic partition ` `    ``currPart ``=` `[] ` ` `  `    ``# Call recursive function to generate  ` `    ``# all partitions and store in allPart ` `    ``allPalPartUtil(allPart, currPart, ``0``, n, string) ` ` `  `    ``# Print all partitions generated by above call ` `    ``for` `i ``in` `range``(``len``(allPart)): ` `        ``for` `j ``in` `range``(``len``(allPart[i])): ` `            ``print``(allPart[i][j], end ``=` `" "``) ` `        ``print``() ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``string ``=` `"nitin"` `    ``allPalPartitions(string) ` ` `  `# This code is contributed by ` `# sanjeev2552 `

Output:

```n i t i n
n iti n
nitin```