Importance of Reading the Problem Carefully

 I was solving this programming challenge at Hackerrank: 

https://www.hackerrank.com/challenges/append-and-delete/problem?h_r=profile

It is called "Append and Delete". So, for say you have [a,b,c] and need to make [d,e,f] in 6 moves. You can delete the last element and add a new letter. 

So you can simply remove all elements in [a,b,c] and add the three new elements.

It is a simple one actually with just lots of edge cases. I implemented the solution, tested and it looked like it was going to work. 

 

Yet it failed. I looked and tried to fix it yet I was failing on a case: 

y, yu, 2 -> should print "no" but my program was saying yes. It does not make sense at first glance. It should be enough. 

I thought I was going crazy at first. Maybe the answers were wrong ? So I looked again and again and re-read the problem again and again. Then the eureka moment; problem states that one needs to do the "exact" amount of moves and not the maximum number of moves!

determine whether or not you can convert "s" to "t" by performing exactly "k" of the above operations on "s".

One word that I missed caused me 30 mins more. It was even in italic font! I corrected my error and saw the tests passing and sanity was restored in the universe.

Below is the end result. Not really optimized but gets the job done.

Comments