# Append and Delete – HackerRank Solution

In this post, we will solve Append and Delete HackerRank Solution. This problem (Append and Delete) is a part of HackerRank Algorithms series.

Contents

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

Example.Â s = [a, b, c]
t = [d, e, f]
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. 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Â 6Â 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Â s, the initial string.
The second line contains a stringÂ t, the desired final string.
The third line contains an integerÂ k, the number of operations.

## Constraints

• 1 <= |s| <= 100
• 1 <= |t| <= 100
• 1 <= k <= 100
• sÂ andÂ tÂ consist of lowercase English letters,Â ascii[a – z].

Sample Input 0

``````hackerhappy
hackerrank
9``````

Sample Output 0

``Yes``

Explanation 0

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

Sample Input 1

``````aba
aba
7``````

Sample Output 1

``Yes``

Explanation 1

We performÂ 4Â delete operations to reduce stringÂ sÂ to the empty string. Recall that though the string will be empty afterÂ 3Â deletions, we can still perform a delete operation on an empty string to get the empty string. Next, we performÂ 3Â append operations (i.e.,Â `a`,Â `b`, andÂ `a`). Because we were able to convertÂ sÂ toÂ tÂ by performing exactlyÂ k = 7Â operations, we returnÂ `Yes`.

Sample Input 2

``````ashley
ash
2``````

Sample Output 2

``No``

Explanation 2

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

## Solution – Append and Delete – HackerRank Solution

### C++

```#include <iostream>
#include <vector>
#include <string>
#include <string.h>
#include <cmath>

using namespace std;

int main() {
string s, t;
cin >> s >> t;
int k;
cin >> k;

int i = 0, j = 0;
for (; i < (int)s.size() && j < (int)t.size(); ++i,++j) {
if (s[i] != t[j])
break;
}

int need = ((int)s.size() - i) + ((int)t.size() - j);
if ((need <= k && (k-need) % 2 == 0) || k >= (int)s.size() + (int)t.size()) {
cout << "Yes";
} else {
cout << "No";
}

return 0;
}
```

### Python

```#!/bin/python3

import sys

def appendAndDelete(s, t, k):
start = 0
ind = 0
to_del = 0
to_app = 0

while ind < len(s) and ind < len(t) and s[ind] == t[ind]:
ind += 1
start = ind

if start < len(s):
to_del = len(s[start:])
if to_del == len(s) and k - to_del >= len(t):
return 'Yes'
if start < len(t):
to_app = len(t[start:])
k -= to_del + to_app

#print("start = {} to_del = {} to_app = {} k = {}".format(start, to_del, to_app, k))
if k == 0 or (k > 0 and k % 2 == 0) or k >= 2*len(t):
return 'Yes'
else:
return 'No'

if __name__ == "__main__":
s = input().strip()
t = input().strip()
k = int(input().strip())
result = appendAndDelete(s, t, k)
print(result)
```

### Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
String t = in.next();
int k = in.nextInt();
int toDelete = 0;
int i = 0;
while (i < s.length() && i < t.length() && s.charAt(i) == t.charAt(i)) {
i++;
}
toDelete = s.length() - i;
int ops = toDelete + (t.length() - i);
if (ops <= k && ((k - ops) % 2 == 0 || (k - ops) > 2 * i)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
```

Note: This problem (Append and Delete) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.