Computer Science, asked by sabithadevikannan24, 5 months ago

Removing characters from a word to form largest possible Palindrome:
Mohan was taught about palindromes at school today and he got fascinated by the idea of palindromes. He started analyzing various words and thought it should be possible to create palindromes from most words by removing a few characters from the word. Write a method to help Mohan fid the length of the largest possible palindrome in a given word, by removing few characters from the given word so that the remaining characters in the word can form a palindrome.
Note: you are not expected to form one or all possible palindrome in the word. You are expected to only find the number of characters that have to be removed from a palindrome, and thus determine the length of the largest possible palindromes.
For Example: if the given word is “Template” if ‘m’,’p,’T’ and ‘a’ are removed we are left with “Tete” which is a good candidate to form a palindrome. In addition, if we let one of the characters ‘m’, ‘p’, ‘l’ or ‘a’ stay within the word, we can still form valid palindromes. For e.g if we remove ‘m’, ‘l’, and ‘a’ but not ‘p’, then the set of characters in the word would be “Tepet” or “Etpte”. So 3 is the number of characters that have to be removed from “Template” so that the remaining characters can form the largest possible palindrome, and thus 5 is the length of the largest possible palindrome.
 
Note 1: If all the characters in the word are already sufficient to form a palindrome, then the length of the largest possible palindrome should be the length of the given word.
For, e.g, if the word is “Magma”, then the result should be 5.
Note 2: If all the characters in the word are different and cannot form a palindrome, then the length of the largest palindrome should be -1.
For e.g: If the word is “victory”, then the result should be -1.
Note 3: Ignore the case of the letters while doing the check i.e “template” or “template” or “TEMplate” or “TEmpPLate” or “TEMPLATE” should all give the same result, which is 5.
Template

5


Magma
5

Victory
-1


Malayalam
9

Welcome
3

Programming
7

Abcdefghij
-1

Aabbcc
6

TamilimaT
9

Zxcvbnm
-1

Engineering
9



Technologies
5

Palindrome
-1​

Answers

Answered by shahv4964
0

Explanation:

download booyah app and log in with Facebook and Google search fronzo this is my icon and follow me

only search fronzo

Attachments:
Similar questions