Maximum number of overlapping string | Java 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
import java . util . * ; class GFG { static int maxOverlap ( String S , String T ) { String str = T ; int count [ ] = new int [ T . length ( ) ] ; int overlap = 0 ; int max_overlap = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { int index = str . indexOf ( S . charAt ( i ) ) ; if ( index == 0 ) { overlap ++ ; if ( overlap >= 2 ) max_overlap = Math . 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 ; } public static void main ( String [ ] args ) { String S = " chcirphirp " ; String T = " chirp " ; System . out . print ( maxOverlap ( S , T ) ) ; } }