import Foundation
func mergeOnOverlap(_ a: String, _ b: String) -> String {
let lenA = a.count
let lenB = b.count
let maxPossibleOverlapLen = min(lenA, lenB)
var overlap = 0
for len in stride(from: maxPossibleOverlapLen, through: 1, by: -1) {
let startA = a.index(a.endIndex, offsetBy: -len)
let prefixB = b.prefix(len)
if a[startA...] == prefixB {
overlap = len
break
}
}
let dropIndex = b.index(b.startIndex, offsetBy: overlap)
return a + b[dropIndex...]
}
let a = "fantasy time travel technology"
let b = "technology extraterrestrial life"
print(mergeOnOverlap(a, b))
/*
run:
fantasy time travel technology extraterrestrial life
*/