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)
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)
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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Return maximum occurring character in an input string
- Print all the duplicates in the input string
- Remove characters from the first string which are present in the second string
- Remove duplicates from a given string
- Print reverse of a string using recursion
- Write a program to print all permutations of a given string
- Divide a string in N equal parts
- Given a string, find its first non-repeating character
- Write a program to reverse an array or string
- Reverse words in a given string
- Find the smallest window in a string containing all characters of another string
- Lexicographic rank of a string
- An in-place algorithm for String Transformation
- Count words in a given string
- String matching where one string contains wildcard characters
- Remove "b" and "ac" from a given string
- Find if a string is interleaved of two other strings | DP-33
- Print all ways to break a string in bracket form
- Maximum length prefix of one string that occurs as subsequence in another
- Rearrange a string so that all same characters become d distance away
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.