Given two strings **A** and **B** consisting of only lowercase letters, the task is to find the minimum number of subsequences required from A to form B.

**Examples:**

Input:A = “abbace” B = “acebbaae”

Output:3

Explanation:

Sub-sequences “ace”, “bba”, “ae” from string A used to form string B

Input:A = “abc” B = “cbacbacba”

Output:7

**Approach:**

- Maintain an array for each character of
**A**which will store its indexes in increasing order. - Traverse through each element of
**B**and increase the counter whenever there is a need for new subsequence. - Maintain a variable
**minIndex**which will show that elements greater than this index can be taken in current subsequence otherwise increase the counter and update the minIndex to -1.

Below is the implementation of the above approach.

## C++

`// C++ program to find the Minimum number ` `// of subsequences required to convert ` `// one string to another ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the no of subsequences ` `int` `minSubsequnces(string A, string B) ` `{ ` ` ` `vector<` `int` `> v[26]; ` ` ` `int` `minIndex = -1, cnt = 1, j = 0; ` ` ` `int` `flag = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < A.length(); i++) { ` ` ` ` ` `// Push the values of indexes of each character ` ` ` `int` `p = (` `int` `)A[i] - 97; ` ` ` `v[p].push_back(i); ` ` ` `} ` ` ` ` ` `while` `(j < B.length()) { ` ` ` `int` `p = (` `int` `)B[j] - 97; ` ` ` ` ` `// Find the next index available in the array ` ` ` `int` `k = upper_bound(v[p].begin(), ` ` ` `v[p].end(), minIndex) ` ` ` `- v[p].begin(); ` ` ` ` ` `// If Character is not in string A ` ` ` `if` `(v[p].size() == 0) { ` ` ` `flag = 1; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Check if the next index is not equal to the ` ` ` `// size of array which means there is no index ` ` ` `// greater than minIndex in the array ` ` ` `if` `(k != v[p].size()) { ` ` ` ` ` `// Update value of minIndex with this index ` ` ` `minIndex = v[p][k]; ` ` ` `j = j + 1; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// Update the value of counter ` ` ` `// and minIndex for next operation ` ` ` `cnt = cnt + 1; ` ` ` `minIndex = -1; ` ` ` `} ` ` ` `} ` ` ` `if` `(flag == 1) { ` ` ` `return` `-1; ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string A1 = ` `"abbace"` `; ` ` ` `string B1 = ` `"acebbaae"` `; ` ` ` `cout << minSubsequnces(A1, B1) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the Minimum number ` `# of subsequences required to convert ` `# one to another ` `from` `bisect ` `import` `bisect as upper_bound ` ` ` `# Function to find the no of subsequences ` `def` `minSubsequnces(A, B): ` ` ` `v ` `=` `[[] ` `for` `i ` `in` `range` `(` `26` `)] ` ` ` `minIndex ` `=` `-` `1` ` ` `cnt ` `=` `1` ` ` `j ` `=` `0` ` ` `flag ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `len` `(A)): ` ` ` ` ` `# Push the values of indexes of each character ` ` ` `p ` `=` `ord` `(A[i]) ` `-` `97` ` ` `v[p].append(i) ` ` ` ` ` `while` `(j < ` `len` `(B)): ` ` ` `p ` `=` `ord` `(B[j]) ` `-` `97` ` ` ` ` `# Find the next index available in the array ` ` ` `k ` `=` `upper_bound(v[p], minIndex) ` ` ` ` ` `# If Character is not in A ` ` ` `if` `(` `len` `(v[p]) ` `=` `=` `0` `): ` ` ` `flag ` `=` `1` ` ` `break` ` ` ` ` `# Check if the next index is not equal to the ` ` ` `# size of array which means there is no index ` ` ` `# greater than minIndex in the array ` ` ` `if` `(k !` `=` `len` `(v[p])): ` ` ` ` ` `# Update value of minIndex with this index ` ` ` `minIndex ` `=` `v[p][k] ` ` ` `j ` `=` `j ` `+` `1` ` ` `else` `: ` ` ` ` ` `# Update the value of counter ` ` ` `# and minIndex for next operation ` ` ` `cnt ` `=` `cnt ` `+` `1` ` ` `minIndex ` `=` `-` `1` ` ` `if` `(flag ` `=` `=` `1` `): ` ` ` `return` `-` `1` ` ` `return` `cnt ` ` ` `# Driver Code ` `A1 ` `=` `"abbace"` `B1 ` `=` `"acebbaae"` `print` `(minSubsequnces(A1, B1)) ` ` ` `# This code is contriuted by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

3

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.

## Recommended Posts:

- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- 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 number of adjacent swaps required to convert a permutation to another permutation by given condition
- Check if it is possible to convert one string into another with given constraints
- Transform One String to Another using Minimum Number of Given Operation
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum cost to traverse from one index to another in the String
- Find length of longest subsequence of one string which is substring of another string
- 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 flips required to convert given string into concatenation of equal substrings of length K
- Minimum operations required to convert all characters of a String to a given Character
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Minimum number of integers required such that each Segment contains at least one of them
- Minimum splits required to convert a number into prime segments
- Minimum steps to convert one binary string to other only using negation
- Convert many to many mappings to maximum no of one to one mappings
- Split the string into minimum parts such that each part is in the another 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 contribute@geeksforgeeks.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.