public static String combine ( String left , String right , int minOverlap ) { if ( left . length ( ) < minOverlap ) return null ; if ( right . length ( ) < minOverlap ) return null ; int bestOffset = Integer . MAX_VALUE ; for ( int offset = minOverlap - left . length ( ) ; offset < right . length ( ) - minOverlap ; offset ++ ) { boolean match = true ; for ( int posLeft = Math . max ( 0 , - offset ) ; posLeft < Math . min ( left . length ( ) , right . length ( ) - offset ) ; posLeft ++ ) { if ( left . charAt ( posLeft ) != right . charAt ( posLeft + offset ) ) { match = false ; break ; } } if ( match ) { if ( Math . abs ( bestOffset ) > Math . abs ( offset ) ) bestOffset = offset ; } } if ( bestOffset < Integer . MAX_VALUE ) { String combined = left ; if ( bestOffset > 0 ) { combined = right . substring ( 0 , bestOffset ) + combined ; } if ( left . length ( ) + bestOffset < right . length ( ) ) { combined = combined + right . substring ( left . length ( ) + bestOffset ) ; } return combined ; } else { return null ; } }
Combine string .