Hello Programmers, In this post, you will learn how to solve HackerRank Append and Delete Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.
You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C Programming, HackerRank C++ Programming, HackerRank Java Programming, HackerRank Python Programming, HackerRank Linux Shell, HackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data Structures, RDBMS Programs, Java Programs Solutions, Fiverr Skills Test answers, Google Course Answers, Linkedin Assessment, and Coursera Quiz Answers.
HackerRank Append and Delete
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 Chase2learn. This tutorial is only for Educational and Learning purposes.
FAQ:
1. How do you solve the first question in HackerRank?
If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.
2. How do I find my HackerRank ID?
You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more
3. Does HackerRank detect cheating?
yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.
4. Does HackerRank use camera?
No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.
5. Should I put HackerRank certificate on resume?
These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume.
6. Can I retake HackerRank test?
The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite.
7. What is HackerRank?
HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi
Finally, we are now, in the end, I just want to conclude some important message for you
Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.
Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe.