Change language

Python | Merge two strings with suffix and prefix

|

You are given two lines A and B, and these lines contain lowercase letters. The challenge is to specify the length of the merged strings. For example, if A — "Abcde", and B — "Cdefg" then concatenation of the two strings results in "abcdefg". The merge operation is performed such that the characters to be appended are suffix A and prefix B.
Before merging, you are allowed to perform ONE of the following operations:

  1. Backward Line A
  2. Backward Line B

Examples :

  Input:  A = "ababc" B = "bcabc"  Output:  Length is 8 the suffix of string A ie "bc" and prefix of B ie "bc" is the same so the merged string will be "ababcabc" and length is 8.  Input:  A = "cdefg" B = "abhgf"  Output:  Length is 8 the suffix of string A ie "fg" and prefix of reversed B ie "fg" is the same so the merged string will be "cdefghba" and length is 8  Input:  A = "wxyz" B = "zyxw"  Output:  Length is 4 

Below is the Python code implementation of the above approach.

# function to find length
# concatenated string

 

def mergedstring (x, y) :

 

k = len (y)

for i in range ( len (x)):

 

if x [i:] = = y [: k]:

break

else :

k = k - 1

  

# uncomment the following statement

# know what a concatenated string is

# print (a + b [k:])

return len (a + b [k:])

  
# function to find the minimum length
# among the concatenated string

def merger (a, b):

# reverse b

b1 = b [:: - 1

 

# function call to determine length

# lines without backlines & # 39; B & # 39;

r1 = mergedstring (a, b)

  

# function call to determine length

# lines by reversing line "B"

r2 = mergedstring (a, b1)

 

# compare between lengths

if r1" r2:

print ( "Length is" , r2)

else :

print ( "Length is" , r1)

 
# driver code

a = "abcbc"

b = "bcabc"

 
merger (a, b)

Exit :

 Length is 8 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically