HackerRank Append and Delete problem solution YASH PAL, 31 July 2024 In this HackerRank Append and Delete problem you have Given an integer, k, and two strings, s, and t, to 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. Problem solution in Python programming. #!/bin/python3 import sys s = input().strip() t = input().strip() k = int(input().strip()) same = 0 i = 0 while i<len(s) and i<len(t) and s[i]==t[i]: i+=1 s1 = len(s[i:]) t1 = len(t[i:]) if s1+t1>k: print("No") elif s1+t1==k: print("Yes") elif (len(s)+len(t))-k<=0: print("Yes") elif abs((len(s)+len(t))-k)%2==0: print("Yes") else: print("No") Problem solution in Java Programming. 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) { final Scanner in = new Scanner(System.in); final String s = in.next(); final String t = in.next(); int k = in.nextInt(); final int sL = s.length(); final int tL = t.length(); int lastCommon = -1; while(lastCommon + 1 < sL && lastCommon + 1 < tL && s.charAt(lastCommon+1) == t.charAt(lastCommon+1)) { lastCommon++; } if(lastCommon == -1) { // If strings are different if(k >= tL + sL) { // If k more then target lenght and remainder is even System.out.println("Yes"); } else { System.out.println("No"); } } else { int sDiff = sL - lastCommon - 1; int tDiff = tL - lastCommon - 1; if(k >= tL + sL) { System.out.println("Yes"); } else if(k >= sDiff + tDiff && (k - sDiff -tDiff)%2 == 0) { System.out.println("Yes"); } else { System.out.println("No"); } } } } Problem solution in C++ programming. #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; } Problem solution in C programming. #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; scanf("%d",&k); int l1,l2; l1=strlen(s); l2=strlen(t); int l; if(l1<=l2) l=l2-l1; else l=l1-l2; int v; for(v=0;s[v]!=' ' && t[v]!=' ';v++) { if(s[v]!=t[v]) break; } if(k>=(l1+l2)) {printf("Yesn"); return 0;} if(k>=(l1+l2-2*v) && (k-l)%2==0) {printf("Yes"); return 0;} printf("Non"); return 0; } Problem solution in JavaScript programming. 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()); for (var i=0; i < s.length; i++) { if (s[i] != t[i]) { break; } } var deletesRequired = s.length - i; var addsRequired = t.length - i; var minRequired = deletesRequired + addsRequired; var max = s.length + t.length; if (k < minRequired || ((k % 2 != minRequired % 2) && k < max)) { console.log("No"); } else { console.log("Yes"); } } algorithm coding problems
I got a shorter solution in js 🙂 function appendAndDelete(s, t, k) { let a = s, moves = 0, empty = false; for (let i = 0; i < Math.max(s.length, t.length); i++) { if (a[i] == t[i]) continue; else if (!a[i] && !!t[i]) { a += t[i]; moves++; } else { if (i == 0) empty = true; a = s.substr(0, i); moves += s.length – i–; } } if (s.length + t.length <= k || moves <= k && ((k – moves) % 2 == 0 || empty == true)) return "Yes"; else return "No";} Not really sure if its more efficient but this converts the string s to t and in the process counts the steps.In the end there is a check for various conditions.