Append and Delete HackerRank Solution in C, C++, Java, Python

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

Append and Delete HackerRank Solution in C

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(){
    char* s = (char *)malloc(512000 * sizeof(char));
    scanf("%s",s);
    int len_s = strlen(s);
    char* t = (char *)malloc(512000 * sizeof(char));
    scanf("%s",t);
    int len_t = strlen(t);
    int k;
    scanf("%d",&k);
    
    int pref_length = 0;
    
    while (s[pref_length] == t[pref_length] && pref_length < len_s && pref_length < len_t) {
        pref_length++;
    }
    
    int diff = k - len_s - len_t + 2*pref_length;
    if (diff < 0) {
        printf("No\n");
    } else if (k >= len_s + len_t) {
        printf("Yes\n");
    } else if (diff % 2 == 0) {
        printf("Yes\n");
    } else {
        printf("No\n");
    }
    return 0;
}

 

Append and Delete HackerRank Solution in C++

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;


int main(){
    string s;
    cin >> s;
    string t;
    cin >> t;
    int k;
    cin >> k;
    int cl=0;
    while(cl

 

Append and Delete HackerRank 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;mk)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 HackerRank 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 HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
        var line = Console.ReadLine();
        var targ = Console.ReadLine();
        int k = int.Parse(Console.ReadLine());

        int n = line.Length, m = targ.Length, i = 0;
        
        if(k >= m+n) {Console.WriteLine("Yes"); return;}
        
        while (i < n && i < m && line[i] == targ[i]) i++;

        int req = m - i + n - i;

        k -= req;

        Console.WriteLine(k >= 0 && k%2==0 ? "Yes" : "No");
    }
}

 

Attempt Append and Delete HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/append-and-delete/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/sherlock-and-squares-hackerrank-solution/

 

 

Leave a Comment