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 (adsbygoogle = window.adsbygoogle || []).push({}); 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\$ (adsbygoogle = window.adsbygoogle || []).push({}); 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 All questions (adsbygoogle = window.adsbygoogle || []).push({}); News 07/12/2022 Microsoft intends to create a superapp with a search engine and messenger 04/12/2022 Chrome OS 108 is released 27/11/2022 The long-awaited ARPlAN app update for Android is out Wiki File handling Python | How to copy data from one Excel sheet to another __dict__ Common xlabel/ylabel for matplotlib subplots __dict__ 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 Python functions Cyclic redundancy check in Python Counters Finding mean, median, mode in Python without libraries cos Python add suffix / add prefix to strings in a list __dict__ Why do I get "Pickle - EOFError: Ran out of input" reading an empty file? Python functions Python - Move item to the end of the list Loops Python - Print list vertically © 2017—2022 Python Engineering Hub EN | ES | DE | FR | IT | RU | TR | PL | PT | JP | KR | CN | HI | NL Python.Engineering is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com Python Loops Counters NumPy NLP Regular Expressions Wiki Tech news Python Wiki StackOverflow PHP JavaScript Books All books Computations Development Cryptography For dummies Big Data document.addEventListener("DOMContentLoaded", () => { let arrayCode = document.querySelectorAll('pre'); arrayCode.forEach(element => { element.classList.add("prettyprint"); }); }); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-Q022WLXW4X'); ```