![]() |
VOOZH | about |
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.
Examples:
Input: A = "aacbe", B = "aceab"
Output: 3
Explanation:
The minimum number of subsequences required for creating A from B is "aa", "cb" and "e".
Input: A = "geeks", B = "geekofthemonth"
Output: -1
Explanation:
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
Greedy Approach:
Below is the implementation of the above approach.
3
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)