Maximum number of overlapping string | C ++ implementation to find the maximum number of occurrence of the overlapping count ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOverlap ( string S , string T ) { string str = T ; int count [ T . length ( ) ] = { 0 } ; int overlap = 0 ; int max_overlap = 0 ; for ( int i = 0 ; i <= S . length ( ) ; i ++ ) { int index = str . find ( S [ i ] ) ; if ( index == 0 ) { overlap ++ ; if ( overlap >= 2 ) max_overlap = max ( overlap , max_overlap ) ; count [ index ] ++ ; } else { if ( count [ index - 1 ] <= 0 ) return -1 ; count [ index ] ++ ; count [ index - 1 ] -- ; } if ( index == 4 ) overlap -- ; } if ( overlap == 0 ) return max_overlap ; else return -1 ; } int main ( ) { string S = " chcirphirp " ; string T = " chirp " ; cout << maxOverlap ( S , T ) ; return 0 ; }