Append and Delete –  HackerRank Problem Solution

Question:

You have a string of lowercase English alphabetic letters. You can perform two types of operations on the string:

1.       Append a lowercase English alphabetic letter to the end of the string.

2.       Delete the last character in the string. Performing this operation on an empty string results in an empty string.

Given an integer,k , and two strings, s and t, determine whether or not you can convert  to  by performing exactly  k of the above operations on s. If it’s possible, print Yes. Otherwise, print No.

For example, strings s =[a,b,c] and t=[d,e,f]. Our number of moves, k=6. To convert s to t, we first delete all of the characters in 3 moves. Next, we add each of the characters of t in order. On the 6th move, you will have the matching string. If there had been more moves available, they could have been eliminated by performing multiple deletions on an empty string. If there were fewer than 6 moves, we would not have succeeded in creating the new string.

Input

hackerhappy
hackerrank
9

Output

YES

Explanation

We perform 5 delete operations to reduce string s to hacker. Next, we perform  4 append operations (i.e., ran, and k), to get hackerrank. Because we were able to convert s to t by performing exactly  k=9 operations, we print Yes.

Here is original problem on HackerRank

Answer:

Solution logic:

If we observe input 1, common part is “Hacker” from both string which consuming 5 moves and reaming for can use to append “Rank” which make string “HappyRank” with 9 moves.

If we had 10 moves, then we would have not able to create a string because we have an extra operation to perform which result creating different string but with 11 moves, we would consume such two extra operations by removing and re-adding the last character.

so if we get the common part from both string and the difference between strings & provided number k is match or even number the number (can be used to removing and re-adding the last character), which can create the string else will not be able to create a string.

Leave a Reply

Your email address will not be published. Required fields are marked *