Minimum number of characters required to be added to a String such that all lowercase alphabets occurs as a subsequence in increasing order | Python3 program for the above approach ; Function to find the LCS of strings S and string T ; Base Case ; Already Calculated State ; If the characters are the same ; Otherwise ; Function to find the minimum number of characters that needs to be appended in the string to get all lowercase alphabets as a subsequences ; String containing all the characters ; Stores the result of overlapping subproblems ; Return the minimum characters required ; Driver Code
import numpy as np NEW_LINE def LCS ( S , N , T , M , dp ) : NEW_LINE INDENT if ( N == 0 or M == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ N ] [ M ] != 0 ) : NEW_LINE INDENT return dp [ N ] [ M ] ; NEW_LINE DEDENT if ( S [ N - 1 ] == T [ M - 1 ] ) : NEW_LINE INDENT dp [ N ] [ M ] = 1 + LCS ( S , N - 1 , T , M - 1 , dp ) ; NEW_LINE return dp [ N ] [ M ] NEW_LINE DEDENT dp [ N ] [ M ] = max ( LCS ( S , N - 1 , T , M , dp ) , LCS ( S , N , T , M - 1 , dp ) ) ; NEW_LINE return dp [ N ] [ M ] NEW_LINE DEDENT def minimumCharacter ( S ) : NEW_LINE INDENT T = " abcdefghijklmnopqrstuvwxyz " ; NEW_LINE N = len ( S ) ; M = len ( T ) ; NEW_LINE dp = np . zeros ( ( N + 1 , M + 1 ) ) ; NEW_LINE return ( 26 - LCS ( S , N , T , M , dp ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " abcdadc " ; NEW_LINE print ( minimumCharacter ( S ) ) ; NEW_LINE DEDENT