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 email@example.com. 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
- Given a string, print all possible palindromic partitions
- Print all the palindromic permutations of given string in alphabetic order
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Make palindromic string non-palindromic by rearranging its letters
- Find the count of palindromic sub-string of a string in its sorted form
- Print Longest Palindromic Subsequence
- Check if a string contains a palindromic sub-string of even length
- Find a palindromic string B such that given String A is a subsequense of B
- Print all palindromic paths from top left to bottom right in a matrix
- Lexicographically first palindromic string
- Count All Palindromic Subsequence in a given String
- Find all palindromic sub-strings of a given string | Set 2
- Count palindromic characteristics of a String
- Find all distinct palindromic sub-strings of a given string
- Number of strings of length N with no palindromic sub string
Improved By : vibhu4agarwal