Thimo1
Thimo1

Reputation: 119

How do I add a string to another string and remove the overlapping part?

How do I add one string to another and remove a part in the middle if it's double?

I really don't know how to explain this but this is what I want to do:

Lets say

string1 = "abcde"

string2 = "cdefg"

How would I create a variable that's string1+string2 but with the "cde" part only once?

I'm looking for something like:

string3 = merge(string1, string2)

That would make string3 "abcdefg" and not "abcdecdefg"

I couldn't find it with google so that's why i'm asking here.

Any help would be greatly appreciated :)

Upvotes: 0

Views: 946

Answers (3)

Karn Kumar
Karn Kumar

Reputation: 8816

This can be simply implemented with python set() as folows..

>>> string1 = "abcde"
>>> string2 = "cdefg"
>>> string3 = string1 + string2
>>> print (''.join(sorted(set(string3))))
abcdefg

Explanation: set() removes the duplicate elements, sorted() sorts, and the join() combines it all back.

Upvotes: -1

enys
enys

Reputation: 533

You can check if there is an overlap first and then append only the non-overlapping parts:

# Find overlap if there is any
for i in range(1, len(string2)):
    if string1.endswith(string2[:i]):
        k = i

# Concatenate strings:
string3 = string1 + (string2 if k is None else string2[k:])

Or even simpler, set k to zero first:

# Find overlap if there is any
k = 0
for i in range(1, len(string2)):
    if string1.endswith(string2[:i]):
        k = i

# Simply concatenate them
string3 = string1 + string2[k:]

Upvotes: 2

Thierry Lathuille
Thierry Lathuille

Reputation: 24232

We can look for each occurence of the first character of s2 in s1, and test if the rest of s1 from this occurence is the start of s2. We need to do that from the start of s1 towards the end, in order to be sure to get the largest possible overlap.

import re

def fusion(s1, s2):
    for m in re.finditer(s2[0], s1):
        if s2.startswith(s1[m.start():]):
            return s1[:m.start()] + s2
    # no overlap found
    return s1 + s2


string1 = "abede"
string2 = "edefg" # overlap: 'ede'

print(fusion(string1, string2))
# abedefg

Upvotes: 1

Related Questions