Monday, July 11, 2011

Given a word, convert it into a palindrome with minimum addition of letters to it. letters can be added anywhere in the word. for eg if yahoo is given result shud be yahoohay.

e.g

ABBA : 0 (already a palindrome)
ABB: 1
FAE: 2
FOO: 1


Simple Algorithm Will be

You need to find the longest palindrome at the end of the string. An algorithm to see if a string is a palindrome can be created by simply running one pointer from the start of the string and one from the end, checking that the characters they refer to are identical, until they meet in the middle. Something like:

function isPalindrome(s):
i1 = 0
i2 = s.length() - 1
while i2 > i1:
if s.char_at(i1) not equal to s.char_at(i2):
return false
increment i1
decrement i2
return true

Try that with the full string. If that doesn't work, save the first character on a stack then see if the remaining characters form a palindrome. If that doesn't work, save the second character as well and check again from the third character onwards.

Eventually you'll end up with a series of saved characters and the remaining string which is a palindrome.

Best case is if the original string was a palindrome in which case the stack will be empty. Worst case is one character left (a one-character string is automatically a palindrome) and all the others on the stack.

The number of characters you need to add to the end of the original string is the number of characters on the stack.

To actually make the palindrome, pop the characters off the stack one-by-one and put them at the start and the end of the palindromic string.

Examples:

String Palindrome Stack Notes
------ ---------- ----- -----
ABBA Y - no characters needed.

String Palindrome Stack Notes
------ ---------- ----- -----
ABB N -
BB Y A one character needed.
ABBA Y - start popping, finished.

String Palindrome Stack Notes
------ ---------- ----- -----
FAE N -
AE N F
E Y AF two characters needed.
AEA Y F start popping.
FAEAF Y - finished.

String Palindrome Stack Notes
------ ---------- ----- -----
FOO N -
OO Y F one character needed.
FOOF Y - start popping, finished.

String Palindrome Stack Notes
------ ---------- ----- -----
HAVANNA N -
AVANNA N H
VANNA N AH
ANNA Y VAH three characters needed.
VANNAV Y AH start popping.
AVANNAVA Y H
HAVANNAVAH Y - finished.

6 comments :

Technofreak said...

What do you say about this:
ABCBPGBBANP with your algorithm....WRONG RESULTS!!!

Unknown said...

@Technofreak Can You tell me What Wrong , its Worst& good Case you have thought of , what the o/p coming according to you ?

isn't the o/p will be ABCBPGBBANPNABBGPBCBA
correct me if i missed something ?

Shashank

Anonymous said...

what abt this-Kw7wh
wont get the answer by your algorithm

venkat said...

simple example, try your algorithm on fft, the result should be tfft not fftff

dd said...

#min insert
#input mohammadsajjadhossain the output is 8


def minInsert(a):
if len(a)==1 or len(a)==0:
return 0
else:
if a[0]==a[len(a)-1]: # a[0]==a[n-1]
return minInsert(a[1:len(a)-1])
else:
return 1+min(minInsert(a[1:]),minInsert(a[:-1]))

print minInsert('Kw7wh')

I think above algo works..

Unknown said...

@dd seems good to me , can u elaborate what minInsert(a[1:]),minInsert(a[:-1]) in last return stmt .??