You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank - Append and Delete Solution

Hackerrank - Append and Delete Solution

Beeze Aal
Beeze Aal

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, , and two strings,  and , determine whether or not you can convert  to  by performing exactly  of the above operations on . If it's possible, print Yes. Otherwise, print No.

For example, strings  and . Our number of moves, . To convert  to , we first delete all of the characters in  moves. Next we add each of the characters of  in order. On the  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  moves, we would not have succeeded in creating the new string.

Function Description

Complete the appendAndDelete function in the editor below. It should return a string, either Yes or No.

appendAndDelete has the following parameter(s):

  • s: the initial string
  • t: the desired string
  • k: an integer that represents the number of operations

Input Format

The first line contains a string , the initial string.
The second line contains a string , the desired final string.
The third line contains an integer , the number of operations.

Constraints

  • and ¬†consist of lowercase English alphabetic letters, .

Output Format

Print Yes if you can obtain string  by performing exactly  operations on . Otherwise, print No.

Sample Input 0

hackerhappy
hackerrank
9

Sample Output 0

Yes

Explanation 0

We perform  delete operations to reduce string  to hacker. Next, we perform  append operations (i.e., r, a, n, and k), to get hackerrank. Because we were able to convert  to  by performing exactly  operations, we print Yes.

Sample Input 1

aba
aba
7

Sample Output 1

Yes

Explanation 1

We perform  delete operations to reduce string  to the empty string (recall that, though the string will be empty after  deletions, we can still perform a delete operation on an empty string to get the empty string). Next, we perform  append operations (i.e., a, b, and a). Because we were able to convert  to  by performing exactly  operations, we print Yes.

Sample Input 2

ashley
ash
2

Sample Output 2

No

Explanation 2

To convert ashley to ash a minimum of  steps are needed. Hence we print No as answer.

Solution in Python

c = 0
s = input()
t = input()
k = int(input())
l = len(s)
while s[:l]!=t[:l]:
    l-=1
    c+=1
o = ((len(t)-l)+c)
if k<o:
    print("No")
elif (len(s)+len(t))<=k:
    print("Yes")
elif 2*len(t)<k:
    print("Yes")
elif k%2 == o%2:
    print("Yes")
else:
    print("No")

Additional Info

o is the character difference between the longer string and the matching part.

Example 1

Between hackerhappy and hackerrank. The matching part is "hacker". There are 6 letters in the string "hacker". Therefore,

>>> o = len("hackerhappy") - len("hacker")
>>> o
5

Example 2

Between ba and bababa. The matching part is "ba". There are 2 letters in the string "ba". Therefore,

>>> o = len("bababa") - len("ba")
>>> o
4

CASE - A

If the number of operations(k) is less than the difference between the string then it is impossible to match String s with String t.

if k<o:
	print("No")

CASE - B

If length of string s + length of string t is greater than or equal to the number of operations then we will always be able to change string s to string t.

if (len(s)+len(t))<=k:
    print("Yes")

As it is given in the question

We can perform a delete operation on an empty string to get the empty string

Let us define two functions one for removing last character and one for adding character at the end

def add_end(s,string):
    return string+s

def rmv_end(string):
    return string[:-1]

Given s = "aba" t = "ab"

>>> len(s)+len(t)
5

We can convert aba to ab using any number of operations greater than 5

5 operations.

>>> s = rmv_end(s)
>>> s = rmv_end(s)
>>> s = rmv_end(s)
>>> s = add_end("a",s)
>>> s = add_end("b",s)
>>> print(s)
ab

6 operations.

>>> s = rmv_end(s)
>>> s = rmv_end(s)
>>> s = rmv_end(s)
>>> s = rmv_end(s)
>>> s = add_end("a",s)
>>> s = add_end("b",s)
>>> print(s)
ab

7 operations.

>>> s = rmv_end(s)
>>> s = rmv_end(s)
>>> s = rmv_end(s)
>>> s = rmv_end(s)
>>> s = rmv_end(s)
>>> s = add_end("a",s)
>>> s = add_end("b",s)
>>> print(s)
ab

As you can see that we can keep removing last character from an empty string to increase the value of k by 1 everytime we do it.