We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
2.Calculate how many characters need to be deleted from s and added to reach t.
3.Check if k is enough to do that and whether the leftover operations can be used meaninglessly.
stringappendAndDelete(strings,stringt,intk){intcommonLength=0;intn1=s.size(),n2=t.size();// Find common prefixfor(inti=0;i<min(n1,n2);i++){if(s[i]==t[i])commonLength++;elsebreak;}inttotalOps=(n1-commonLength)+(n2-commonLength);// Case 1: Enough operations and parity matchesif(totalOps==k)return"Yes";// Case 2: More operations than neededif(totalOps<k){// Check if we can use extra operations meaninglesslyif((k-totalOps)%2==0||k>=n1+n2)return"Yes";}return"No";}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Append and Delete
You are viewing a single comment's thread. Return to all comments →
Step-by-step Solution in C++ :-
1.Find the common prefix length.
2.Calculate how many characters need to be deleted from s and added to reach t.
3.Check if k is enough to do that and whether the leftover operations can be used meaninglessly.