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.
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.
- Form N-copy string with add, remove and append operations
- Sub-strings that start and end with one character and have at least one other
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Minimum steps to convert one binary string to other only using negation
- Check if one string can be converted to other using given operation
- std::string::append vs std::string::push_back() vs Operator += in C++
- Ways of transforming one string to other by removing 0 or more characters
- Check if characters of one string can be swapped to form other
- Find indices of all occurrence of one string in other
- Check if max occurring character of one string appears same no. of times in other
- Number of substrings of one string present in other
- Append a digit in the end to make the number equal to the length of the remaining string
- How to Append a Character to a String in C
- Minimize count of given operations required to make two given strings permutations of each other
- Converting Decimal Number lying between 1 to 3999 to Roman Numerals
- Converting Roman Numerals to Decimal lying between 1 to 3999
- Count of primes after converting given binary number in base between L to R
- Given two numbers as strings, find if one is a power of other
- Check if two strings can be made equal by swapping one character among each other
- Print all possible combinations of the string by replacing '$' with any other digit from the string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.