Hello Programmers, In this post, you will know how to solve the HackerRank Append and Delete Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Append and Delete Solution
Task
You have two strings of lowercase English letters. You can perform two types of operations on the first string:
- Append a lowercase English letter to the end of the string.
- 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
orNo
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.
HackerRank Append and Delete Solution
Append and Delete Solution in C
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ char* s = (char *)malloc(512000 * sizeof(char)); scanf("%s",s); char* t = (char *)malloc(512000 * sizeof(char)); scanf("%s",t); int k,i=0,l1,l2,del,append,same; scanf("%d",&k); l1=strlen(s),l2=strlen(t); if(strcmp(s,t)==0) { if(k%2==0 || k>=2*l1) printf("Yes"); else printf("No"); } else { if(k>=2*l2) printf("Yes"); else { while(i<l1 && i<l2) { if(s[i]==t[i]) i++; else break; } same=i; del=l1-same; append=l2-same; if(del+append > k) printf("No"); else { if((del+append)%2==0) { if(k%2==0) printf("Yes"); else printf("No"); } else { if(k%2==0) printf("No"); else printf("Yes"); } } } } return 0; }
Append and Delete Solution in Cpp
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int main(){ string s; cin >> s; string t; cin >> t; int k; cin >> k; int cl=0; while(cl<s.size() && cl<t.size()){ if(s[cl]!=t[cl]) break; cl++; } if(s.size()-cl+t.size()-cl<=k&& (s.size()-cl+t.size()-cl)%2==k%2){ cout<<"Yes"<<endl; } else if(s.size()+t.size()<=k){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; return 0; }
Append and Delete Solution in 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 sl=s.length();int tl=t.length(); int ll=sl>tl?tl:sl; int m; for(m=0;m<ll;m++) { if(s.charAt(m)!=t.charAt(m))break; } int sleft=sl-m; int tleft=tl-m; int flag=0; if(sleft+tleft>k)flag=1; else { int sub=k-(sleft+tleft); if((sub%2!=0) && !(sub>2*m))flag=1; } if(flag==0) System.out.println("Yes"); else System.out.println("No"); }
Append and Delete Solution in Python
#!/bin/python import sys s = raw_input().strip() t = raw_input().strip() k = int(raw_input().strip()) prefix = 0 for c1, c2 in zip(s, t): if c1 == c2: prefix += 1 else: break if k >= len(s) + len(t): print "Yes" elif k >= len(s) + len(t) - 2 * prefix and k % 2 == (len(s) + len(t)) % 2: print "Yes" else: print "No"
Append and Delete Solution using JavaScript
process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var s = readLine(); var t = readLine(); var k = parseInt(readLine()); let result = 'No'; let similar = 0; for (; similar < s.length; similar++) { if (s[similar] !== t[similar]) { break; } } s = s.slice(similar); t = t.slice(similar); k -=s.length; if (t.length === 0) { result = 'Yes'; // we can delete whatever times we want } else if (k === t.length) { result = 'Yes'; // just add characters one by one } else if (k > t.length && (k - t.length) % 2 == 0) { result = 'Yes'; // build string, then perform pairs of add-remove } else if (k > t.length + 2*similar) { result = 'Yes'; // delete from empty line `k > t.length` times, then add string } process.stdout.write(result); }
Append and Delete Solution in Scala
object Solution { def main(args: Array[String]) { val sc = new java.util.Scanner (System.in); var s = sc.next(); var t = sc.next(); var k = sc.nextInt(); val prefix = (0 until Math.min(s.length, t.length)).takeWhile(i => s(i) == t(i)).size val offPrefix = s.length + t.length - 2*prefix if (k < offPrefix) println ("No") else { val diff = k - offPrefix if (diff < 2*prefix) println (if (diff % 2 == 0) "Yes" else "No") else println("Yes") } } }
Append and Delete Solution in Pascal
program B; uses Math; var S, T: AnsiString; k: Integer; l: Integer; i: Integer; Flag: Boolean; begin ReadLn(S); ReadLn(T); ReadLn(k); Flag := False; for i := k downto 0 do begin l := max(0, Length(S) - i); if Copy(S, 1, l) = Copy(T, 1, l) then begin if k - i = Length(T) - l then begin Flag := True; break; end; // WriteLn(i); // WriteLn(Copy(S, 1, max(0, Length(S) - i))); // WriteLn(Copy(T, 1, max(0, Length(S) - i))); end; end; if Flag then WriteLn('Yes') else WriteLn('No'); end.
Disclaimer: This problem (Append and Delete) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.