Given a string S, determine the least number of characters that should be added on to the left side of S so that the complete string becomes a palindrome.
Input: S = "LOL" Output: 0 LOL is already a palindrome Input: S = "JAVA" Output: 3 We need to add 3 characters to form AVAJAVA.
The idea is to find the longest palindromic prefix of given string. The count of characters after the prefix is our answer. The longest palindromic prefix can be found by looping from last char to first char. For example, in “JAVA”, the longest palindromic prefix is “J”, so we need to add remaining 3 at the beginning characters to form palindrome.
Time Complexity: O(n2)
Thanks to Utkarsh Trivedi for suggesting this solution.
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.
- Minimum insertions to form a palindrome | DP-28
- Minimum insertions to form a palindrome with permutations allowed
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Rearrange characters to form palindrome if possible
- Check if a string can be rearranged to form special palindrome
- Check if characters of a given string can be rearranged to form a palindrome
- Count pair of strings whose concatenation of substrings form a palindrome
- Check if the characters in a string form a Palindrome in O(1) extra space
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Minimum removal to make palindrome permutation
- Minimum cost to convert string into palindrome
- Count minimum swap to make string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of deletions to make a string palindrome
- Minimum reduce operations to convert a given string into a palindrome
- Minimum number of Appends needed to make a string palindrome
- Minimum characters to be added at front to make string palindrome
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Form minimum number from given sequence
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
Improved By : Smitha Dinesh Semwal