If it doesn't have to be optimal and you dont have to actually construct it, then what's the point? you can just return length() * 2 - 1, which is the result of appending itself in reverse (minus the last character)
abcd -> abcdcba
Quote (Portgas @ Nov 8 2012 08:41am)
I have thought of this algorithm but im not sure if its a greedy. What you guys think?
- gets the word.
- make a copy of the same word on another string but backwards.
- compare both words.
- if ( they are equal ) return;
- else remove one letter from the beggining of normal word, and one latter from the end of the backwards word.
Repeat that process until the "if" condition is satisfied. Then it will find the last palindrome that exists on the word. It's not optimal, but it works.
Quote (Portgas @ Nov 8 2012 09:14am)
It does satisfy, but not sure about its greedyness.
You can't remove letters, just add. In this case my algorithm does not add letters in the middle of the word. Just in the and.
You don't have to form the palyndrome, you have to return the number of letters to be isnerted in the string that makes it a palindrome. Does not have to be optimal, or in other words, it doesnt need to return the least number of letters to insert on that word
to make it palindrome. Got it?
wat