Longest substring that starts with X and ends with Y | Function returns length of longest substring starting with X and ending with Y ; Length of string ; Find the length of the string starting with X from the beginning ; Find the length of the string ending with Y from the end ; Longest substring ; Print the length ; Driver Code ; Given string str ; Starting and Ending characters ; Function Call
def longestSubstring ( str , X , Y ) : NEW_LINE INDENT N = len ( str ) NEW_LINE start = 0 NEW_LINE end = N - 1 NEW_LINE xPos = 0 NEW_LINE yPos = 0 NEW_LINE while ( True ) : NEW_LINE INDENT if ( str [ start ] == X ) : NEW_LINE INDENT xPos = start NEW_LINE break NEW_LINE DEDENT start += 1 NEW_LINE DEDENT while ( True ) : NEW_LINE INDENT if ( str [ end ] == Y ) : NEW_LINE INDENT yPos = end NEW_LINE break NEW_LINE DEDENT end -= 1 NEW_LINE DEDENT length = ( yPos - xPos ) + 1 NEW_LINE print ( length ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " HASFJGHOGAKZZFEGA " NEW_LINE X = ' A ' NEW_LINE Y = ' Z ' NEW_LINE longestSubstring ( str , X , Y ) NEW_LINE DEDENT