Given two strings A and B consists of lowercase letters, the task to find the minimum number of subsequence required to form A from B. If it is impossible to form, print -1.
Input: A = “aacbe”, B = “aceab”
The minimum number of subsequences required for creating A from B is “aa”, “cb” and “e”.
Input: A = “geeks”, B = “geekofthemonth”
It is not possible to create A, as the character ‘s’ is missing in B.
For brute-force approach refer here
Minimum number of subsequences required to convert one string to another
- Create a 2D-Array of 26 * size_of_string_B to store the occurrence of indices of the character and initialize the array with ‘infinite’ values.
- Maintain the 2D Array with indices of the character in the String B
- If the value of any element of the 2D Array is infinite, then update the value with the next value in the same row.
- Initialize the position of the pointer to 0
- Iterate the String A and for each character –
- If the position of the pointer is 0 and if that position in the 2D Array is infinite, then the character is not present in the string B.
- If the value in the 2D Array is not equal to infinite, then update the value of the position with the value present at the 2D Array for that position of the pointer, Because the characters before it cannot be considered in the subsequence.
Below is the implementation of the above approach.
Time Complexity: O(N), where N is the length of string to be formed (here A)
Auxiliary Space Complexity: O(N), where N is the length of string to be formed (here A)
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.
- Minimum number of subsequences required to convert one string to another
- Minimum swaps required to convert one binary string to another
- Minimum number of given operations required to convert a string to another string
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Minimum substring flips required to convert given binary string to another
- Greedy Algorithm to find Minimum number of Coins
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Check if a string can be obtained by appending subsequences of another string
- Transform One String to Another using Minimum Number of Given Operation
- Check if it is possible to convert one string into another with given constraints
- Convert one array to another using adjacent swaps of elements
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Graph Coloring | Set 2 (Greedy Algorithm)
- Greedy Algorithm for Egyptian Fraction
- K Centers Problem | Set 1 (Greedy Approximate Algorithm)
- Set Cover Problem | Set 1 (Greedy Approximate Algorithm)
- Boruvka's algorithm | Greedy Algo-9
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Minimum cuts required to convert a palindromic string to a different palindromic 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.