Check if a string can be converted to another given string by removal of a substring | Function to check if S can be converted to T by removing at most one substring from S ; Check if S can be converted to T by removing at most one substring from S ; Stores length of string T ; Stores length of string S ; Iterate over the range [ 0 , M - 1 ] ; Stores Length of the substring S [ 0 ] , ... , S [ i ] ; Stores Length of the substring S [ 0 ] , ... , S [ i ] ; Stores prefix substring ; Stores suffix substring ; Checking if prefix + suffix == T ; Given String S and T ; Function call
def make_string_S_to_T ( S , T ) : NEW_LINE INDENT possible = False NEW_LINE M = len ( T ) NEW_LINE N = len ( S ) NEW_LINE for i in range ( 0 , M + 1 ) : NEW_LINE INDENT prefix_length = i NEW_LINE suffix_length = M - i NEW_LINE prefix = S [ : prefix_length ] NEW_LINE suffix = S [ N - suffix_length : N ] NEW_LINE if ( prefix + suffix == T ) : NEW_LINE INDENT possible = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( possible ) : NEW_LINE INDENT return " YES " NEW_LINE DEDENT else : NEW_LINE INDENT return " NO " NEW_LINE DEDENT DEDENT S = " ababcdcd " NEW_LINE T = " abcd " NEW_LINE print ( make_string_S_to_T ( S , T ) ) NEW_LINE