public static String intersectingConcatenate(String a, String b) {

    // find length of maximum possible match
    int lenOfA = a.length();
    int lenOfB = b.length();
    int minIntersectionLen = min(lenOfB, lenOfA);

    // search down from maximum match size, to get longest possible intersection
    for (int size = minIntersectionLen; size > 0; size--) {
      if (a.regionMatches(lenOfA - size, b, 0, size)) {
        return a + b.substring(size, lenOfB);
      }
    }

    // Didn't find any intersection. Fall back to straight concatenation.
    return a + b;
  }
public static String intersectingConcatenate ( String a , String b ) { int lenOfA = a . length ( ) ; int lenOfB = b . length ( ) ; int minIntersectionLen = min ( lenOfB , lenOfA ) ; for ( int size = minIntersectionLen ; size > 0 ; size -- ) { if ( a . regionMatches ( lenOfA - size , b , 0 , size ) ) { return a + b . substring ( size , lenOfB ) ; } } return a + b ; }
include the intersection/overlap only once.