Given two strings S and T of same length. The task is to determine whether or not we can build a string A(initially empty) equal to string T by performing the below operations.
- Delete the first character of S and add it at the front of A.
- Delete the first chatacter of S and add it at the back of A.
Input: S = “abab” T = “baab”
Add ‘a’ at front of A, then A = “a” and S = “bab”
Add ‘b’ at front of A, then A = “ba” and S = “ab”
Add ‘a’ at back of A, then A = “baa” and S = “b”
Add ‘b’ at back of A, then A = “baab” and S = “”
So we can make string A equal to string T
Input: S = “geeks” T = “Teeks”
Approach: The idea is to use Dynamic Programming to solve this problem.
There are two possible moves for every character( front move or back move ). So, for each character, we will check if it is possible to add the character in the front or back of the new string. If it’s possible, we will move to the next character. If it’s not possible, then the operation will stop at that point and No will be printed.
- Firstly we will make a 2D boolean array dp having rows and columns equal to the length of string S, where dp[i][j] = 1 indicates that all characters of string S from index i to n-1 can be placed in the new string A with j front moves such that it becomes equal to string T.
- We can traverse string S from the back and for each character update dp in two ways, if we take the (i-1)-th character as a front move or (i-1)-th character as a back move.
- Finally, we will check if any value at the 1st row is equal to one or not.
Below is the implementation of the above approach
Time Complexity: O(N2)
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.
- Check if N can be converted to the form K power K by the given operation
- Check if one string can be converted to another
- Check if a given string can be converted to another by given possible swaps
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Check if a String can be converted to Pangram in K changes
- Check whether str1 can be converted to str2 with the given operations
- Check if characters of one string can be swapped to form other
- Check if two strings can be made equal by swapping one character among each other
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Check whether the string S1 can be made equal to S2 with the given operation
- Transform One String to Another using Minimum Number of Given Operation
- Check if given string can be formed by two other strings or their permutations
- Sub-strings that start and end with one character and have at least one other
- Check if max occurring character of one string appears same no. of times in other
- Check if end of a sorted Array can be reached by repeated jumps of one more, one less or same number of indices as previous jump
- Largest even number possible by using one swap operation in given number
- Minimum steps to convert one binary string to other only using negation
- Converting one string to other using append and delete last operations
- Form the smallest number using at most one swap operation
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.