Monday 28 December 2020

Hacker Rank Append and Delete solution in c

Problem:

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

Solution:

#include<stdio.h>

#include<string.h>

int main()
{
    char s[101],t[101];
    int k;
    scanf(" %s",s);
    scanf(" %s",t);
    scanf("%d",&k);
    int ls=strlen(s);
    int lt=strlen(t),i,ans;
    for(i=0;s[i]!='\0'&&t[i]!='\0';i++)
    {
        if(s[i]!=t[i])
            break;
    }
    ans=ls-i+lt-i;
        
    if(ans ==k||(ans < k && ans%2 == k%2)||k>=ls+lt)
        printf("Yes");
    else
        printf("No");
    
}

consider this testcase
y
yu
2
Here we can perform only one operation that is appending u
but given k is 2
so for this test case output must be No
so this is taken care by the condition
number of operations required and k must be even
as delete and append are two operations.

Also when k>=ls+lt, we can remove entire s string and append t string
as we can delete an empty string as many times as we like..so when k>=ls+lt,
output is always "YES"


No comments:

Post a Comment