- Append and Delete

 


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

  1. Append a lowercase English letter to the end of the string.
  2. Delete the last character of 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.

Example

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. Return Yes.

If there were 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):

  • string s: the initial string
  • string t: the desired string
  • int k: the exact number of operations that must be performed

Returns

  • string: either Yes or No

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 letters, .

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., ran, and k), to get hackerrank. Because we were able to convert  to  by performing exactly  operations, we return 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., ab, and a). Because we were able to convert  to  by performing exactly  operations, we return 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.

Program:

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the appendAndDelete function below.
def appendAndDelete(s, t, k):
    s_list=list(s)
    t_list=list(t)
    t=0
    f=0
    
    if len(s_list)>len(t_list):
        for i in range(0,len(t_list)):
            if s_list[i]!=t_list[i]:
                t=i
                f=1
                break
        if f==1:
            l=len(s_list)-t
            m=len(t_list)-t
        
            if (l+m)<=k:
                return 'Yes'
            else:
                return 'No'
        else:
            l=len(s_list)-len(t_list)
            if l<=k:
                return 'Yes'
            else:
                return 'No'
    elif len(s_list)<len(t_list):
        for i in range(0,len(s_list)):
            if s_list[i]!=t_list[i]:
                t=i
                f=1
                break
        if f==1:
            l=len(s_list)-t
            m=len(t_list)-t
        
            if (l+m)<=k:
                return 'Yes'
            else:
                return 'No'
        else:
            l=len(t_list)-len(s_list)
            if l<=k:
                return 'Yes'
            else:
                return 'No'
    else:
        for i in range(0,len(t_list)):
            if s_list[i]!=t_list[i]:
                t=i
                f=1
                break
        if f==1:
            l=len(s_list)-t
            m=len(t_list)-t
        
            if (l+m)<=k:
                return 'Yes'
            else:
                return 'No'
        else:
            l=len(s_list)-len(t_list)
            if l<=k:
                return 'Yes'
            else:
                return 'No'
                 

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    t = input()

    k = int(input())

    result = appendAndDelete(s, t, k)

    fptr.write(result + '\n')

    fptr.close()

Output:

Compiler Message
Success
Input (stdin)
  • hackerhappy
  • hackerrank
  • 9
Expected Output
  • Yes

Post a Comment

Previous Post Next Post