Given a string s, partition s such that every string of the partition is a palindrome. Return all possible palindrome partitioning of s.
Input : s = "bcc" Output : [["b", "c", "c"], ["b", "cc"]] Input : s = "geeks" Output : [["g", "e", "e", "k", "s"], ["g", "ee", "k", "s"]]
We have to list the all possible partitions so we will think in the direction of recursion. When we are on index i, we incrementally check all substrings starting from i for being palindromic. If found, we recursively solve the problem for the remaining string and add this in our solution.
Following is the solution-
- We will maintain a 2-dimensional vector for storing all possible partitions and a temporary vector for storing the current partition, new starting index of string to check partitions as we have already checked partitions before this index.
- Now keep on iterating further on string and check if it is palindrome or not.
- If it is a palindrome than add this string in current partitions vector. Recurse on this new string if it is not the end of the string. After coming back again change the current partition vector to the old one as it might have changed in the recursive step.
- If we reach the end of string while iterating than we have our partitions in our temporary vector so we will add it in results.
To check whether it’s a palindrome or not, iterate on string by taking two pointers. Initialize the first to start and other to end of string. If both characters are same increase the first and decrease the last pointer and keep on iterating until first is less than last one.
g e e k s g ee k s
Dynamic Programming | Set 17 (Palindrome Partitioning)
This article is contributed by Anshul Goyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Given a string, print all possible palindromic partitions
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Make palindromic string non-palindromic by rearranging its letters
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Print all the palindromic permutations of given string in alphabetic order
- Print all possible palindromic string formed using any pair of given strings
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Print all palindromic paths from top left to bottom right in a matrix
- Print all Palindromic Levels Of a Binary Tree
- Print the longest palindromic prefix of a given string
- Maximize partitions such that no two substrings have any common character
- Find all distinct palindromic sub-strings of a given string
- Count All Palindromic Subsequence in a given String
- Find all palindromic sub-strings of a given string | Set 2
- Lexicographically all Shortest Palindromic Substrings from a given string
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Find a palindromic string B such that given String A is a subsequense of B
- Check if a string contains a palindromic sub-string of even length
- Find the count of palindromic sub-string of a string in its sorted form