0

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 :)

1
  • 3
    Work it out on paper first. See what the steps are. Then write the code. Commented Sep 29, 2018 at 14:45

3 Answers 3

2

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:]
Sign up to request clarification or add additional context in comments.

1 Comment

With string1 = "abede" and string2 = "edefg", you get 'abedeedefg' instead of 'abedefg'.
1

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

Comments

-1

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.

1 Comment

with string1 = "abedde", string2 = "eddefghi" you get 'abdefghi' instead of 'abeddefghi'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.