Please explain the logic for doing this question.
It is based on popularly known dynamic programming problem edit distance.So basically in that problem u can delete replace as well as insert the character.Here is the link where u can find both recursive as well as dp solution.U just need to make few changes the solution in the links assumes equal cost of insert delete and replace operation u need to remove delete operation and also change cost of insert and delete operation and then your code will work fine.
Happy Coding
link text
But the accepted solution does not contains this approach.Instead they have iteratively solved it.
for the test 1 abc pcg accepted solutions give O/P 3 but shouldn’t the answer be 1? add g to the end of 1st string and b in the 2nd string between p and c. now the strings become abcg pbcg with cost 0. then change p to a. hence total cost 1.