Given two strings A and B consisting 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 occurence of indices of the character and intialize the array with ‘infinite’ values.
- Maintain the 2D Array with indices of the character in the String B
- If value of any element of the 2D Array is infinite then update the value with the next value in the same row.
- Intialize the position of the pointer to 0
- Iterate the String A and for each character –
- If the position of 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 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)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum number of subsequences required to convert one string to another
- Minimum number of given operations required to convert a string to another string
- Greedy Algorithm to find Minimum number of Coins
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum given operations required to convert a given binary string to all 1's
- Minimum swaps required to convert one binary string to another
- Minimum splits required to convert a number into prime segments
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Minimum number of swaps required to make the string K periodic
- Minimum number of adjacent swaps to convert a string into its given anagram
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Minimum changes required to make first string substring of second string
- Number of K length subsequences with minimum sum
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.