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 a string can be converted to another string by replacing vowels and consonants
- Check if one string can be converted to another
- Check if a String can be converted to Pangram in K changes
- Check whether the string S1 can be made equal to S2 with the given operation
- Check whether str1 can be converted to str2 with the given operations
- Generate number with given operation and check if it is palindrome
- Reduce the string to minimum length with the given operation
- Sub-string that contains all lowercase alphabets after performing the given operation
- Transform One String to Another using Minimum Number of Given Operation
- Print the final string when minimum value strings get concatenated in every operation
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Check if a string can be obtained by rotating another string 2 places
- Check if any permutation of string is a K times repeated string
- Queries to check if string B exists as substring in string A
- Check if a string can be obtained by rotating another string d places
- Check if left and right shift of any string results into given string
- Check if a string can become empty by recursively deleting a given sub-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 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.