Append and Delete


Problem Statement :


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].



Solution :



title-img


                            Solution in C :

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;
}













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;
}













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");
        }
    }
}










python 3:

#!/bin/python3

s = input().strip()
t = input().strip()
k = int(input().strip())

ls = len(s)
lt = len(t)

lcp = 0 # Length of common prefix
while lcp <= ls-1 and lcp <= lt-1 and s[lcp] == t[lcp]:
    lcp += 1

if k >= ls + lt:
    print ("Yes")
elif k >= ls + lt - 2*lcp and (k - ls - lt + 2*lcp)%2 == 0:
    print ("Yes")
else:
    print ("No")
                        








View More Similar Problems

Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →