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
- Palindrome Partitioning | DP-17
- Given a number, find the next smallest palindrome
- Minimum insertions to form a palindrome | DP-28
- Print all ways to break a string in bracket form
- Check if a given string is a rotation of a palindrome
- Find if an array of strings can be chained to form a circle | Set 1
- Online algorithm for checking palindrome in a stream
- Shortest Superstring Problem
- Transform One String to Another using Minimum Number of Given Operation
- Minimum number of bracket reversals needed to make an expression balanced
- Form minimum number from given sequence
- Print all palindrome permutations of a string
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Print shortest path to print a string on screen
- Palindrome Substring Queries
Improved By : Smitha Dinesh Semwal