Given an integer k and two strings str1 and str2 determine whether or not we can convert str1 to str2 by performing exactly k of the below operations on str1. a) Append a lowercase English alphabetic letter to the end of the str1. b) Delete the last character in str1 (Performing this operation on an empty string results in an empty string) Examples:
Input : k = 7, str1 = aba, str2 = aba
Output : Yes
(4 operations to convert str1 to an
empty string(to make string empty we
have to perform one more delete
operation) and 3 append operations)
Input : k = 5, str1 = pqruvs, str2 = pqrxy
Output : Yes
(3 delete operations and 2 append operations)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
First of all we determine the common prefix of both strings and then depending upon the value of common prefix, str1.length, str2.length and k we can conclude result. Below are the cases. CASE A: Cases where we can change str1 to str2 :
If str1.length + str2.length <= k then we can delete str1 completely and re-construct str2 easily.
If [k-(str1.length-prefix.length + str2.length-prefix.length)] is even then we can easily construct str2 from str1. This is because str1.length-prefix.length is number of letter to be deleted and str2.length-prefix.length is number of letter to be added in str1 after deletion of uncommon letter. After this if the operations left is even then we can add and remove any random letter which cost even number of operations.
CASE B: In rest of all cases we cannot construct str2 from str1.