C++ Solution: Append and Delete Hackerrank

Append and Delete


Code

#include <bits/stdc++.h

#include <string>

using namespace std;


int main()

{

    string s,t;

    int k,i,x,y,z;

    cin>>s>>t>>k;                     // taking input 


    x=s.size();                     // calculate the size of first string

    y=t.size();                     //calculate the size of second string

    z=x;


    if((x+y)<=k) // if the sum of sizes of both string is less than the allowed number of operations then the operation is achievable  

    {

        cout<<"Yes";

    }

    else if(x<k) // if the size of first string is greater than the allowed number of operations then the operation is not achievable 

    {

        cout<<"No";

    }

    else // as the above condition are not satisfied then we have to check using loops

    {

        for(i=0;i<z;i++)

        {

            if(s[i]==t[i])        // if the character is same then there is no need of change hence x and y can be decrease  

            {

                x--;

                y--;    

            }

            else

            {

                break;

            }

        }

        if(x+y<=k) // if the sum of sizes after performing the above loop is less than the allowed number of operations then the operation is achievable  

        {

            cout<<"Yes\n";

        }

        else

        {

            cout<<"No\n";

        }

    }

    return 0;

}


Append and delete hackerrank solution in C++

You can post your own solutions in any language in comments section we will review and post with your credits.

Post a Comment

0 Comments