VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-number-of-subsequences-required-to-convert-one-string-to-another/

⇱ Minimum number of subsequences required to convert one string to another - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum number of subsequences required to convert one string to another

Last Updated : 12 Jul, 2025

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:
Explanation: 
Sub-sequences "ace", "bba", "ae" from string A used to form string B

Input: A = "abc" B = "cbacbacba" 
Output:
 

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. 


Output: 
3

 

Time Complexity: O(N1+N2) // N1 is the length of string A and N2 is the length of string B

Auxiliary Space: O(26)

Comment
Article Tags: