Given a string str, we need to find the no. of positions where a letter(lowercase) can be inserted so that string becomes a palindrome.
Input : str = "abca" Output : possible palindromic strings: 1) acbca (at position 2) 2) abcba (at position 4) Hence, the output is 2. Input : str = "aaa" Output : possible palindromic strings: 1) aaaa 2) aaaa 3) aaaa 4) aaaa Hence, the output is 4.
Naive Approach:This approach is to insert all 26 alphabets at every position possible i.e., N+1 positions and check at every position if this insertion makes it a palindrome and increase the count.
First you have to observe that we have to make insertion only at the point when the character at that point violates the palindrome condition i.e., . Now, there will be two cases based on the above fact:
Case I: What if the given string is already a palindrome
Then we can only insert at the position such that the insertion does not violate the palindrome.
1) If the length is even then we can always insert any letter in the middle.
2) If the length is odd then we can insert the letter which is in middle, to the left or right to it.
3) In both the cases we can insert the letter which is in middle(let it be ‘CH’), at positions equals to:
(no.of consecutive CH’s to the left of middle letter)*2.
Case II:If it is not a palindrome
As mentioned above we should start inserting at position where , So we increase the count and check for the cases if insertion at any other position makes it a palindrome.
1) If is a palindrome, then we can insert* at any position before until , K in range .(*letter = S[N-i-1])
2.)If is a palindrome, then we can insert* at any position after until , K in range .(*letter = S[i])
In all the cases we keep increasing the count.
This article is contributed by Harsha Mogali. 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.
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.
- Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Print the arranged positions of characters to make palindrome
- String containing first letter of every word in a given string with spaces
- Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged
- Count of characters in str1 such that after deleting anyone of them str1 becomes str2
- Get the first letter of each word in a string using regex in Java
- Check if the string contains consecutive letters and each letter occurs exactly once
- First uppercase letter in a string (Iterative and Recursive)
- Get K-th letter of the decoded string formed by repeating substrings
- Program to accept String starting with Capital letter
- Move all occurence of letter 'x' from the string s to the end using Recursion
- Ways to remove one element from a binary string so that XOR becomes zero
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Find letter's position in Alphabet using Bit operation
- Minimum letters to be removed to make all occurrences of a given letter continuous
- Check if characters of a given string can be rearranged to form a palindrome