Maximum number of overlapping string | Function to find the maximum overlapping strings ; Get the current character ; Condition to check if the current character is the first character of the string T then increment the overlapping count ; Condition to check previous character is also occurred ; Update count of previous and current character ; Condition to check the current character is the last character of the string T ; Condition to check the every subsequence is a valid string T ; Driver Code ; Function Call
def maxOverlap ( S , T ) : NEW_LINE INDENT str = T NEW_LINE count = [ 0 for i in range ( len ( T ) ) ] NEW_LINE overlap = 0 NEW_LINE max_overlap = 0 NEW_LINE for i in range ( 0 , len ( S ) ) : NEW_LINE INDENT index = str . find ( S [ i ] ) NEW_LINE if ( index == 0 ) : NEW_LINE INDENT overlap += 1 NEW_LINE if ( overlap >= 2 ) : NEW_LINE INDENT max_overlap = max ( overlap , max_overlap ) NEW_LINE DEDENT count [ index ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( count [ index - 1 ] <= 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT count [ index ] += 1 NEW_LINE count [ index - 1 ] -= 1 NEW_LINE DEDENT if ( index == 4 ) : NEW_LINE INDENT overlap -= 1 NEW_LINE DEDENT DEDENT if ( overlap == 0 ) : NEW_LINE INDENT return max_overlap NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT S = " chcirphirp " NEW_LINE T = " chirp " NEW_LINE print ( maxOverlap ( S , T ) ) NEW_LINE