# Check whether given string can be generated after concatenating given strings in Python

PythonServer Side ProgrammingProgramming

Suppose we have two strings s and t and r, we have to check whether r = s | t or r = t + s where | denotes concatenation.

So, if the input is like s = "world" t = "hello" r = "helloworld", then the output will be True as "helloworld" (r) = "hello" (t) | "world" (s).

To solve this, we will follow these steps −

• if size of r is not same as the sum of the lengths of s and t, then
• return False
• if r starts with s, then
• if r ends with t, then
• return True
• if r starts with t, then
• if r ends with s, then
• return True
• return False

Let us see the following implementation to get better understanding −

## Example Code

Live Demo

def solve(s, t, r):
if len(r) != len(s) + len(t):
return False

if r.startswith(s):
if r.endswith(t):
return True

if r.startswith(t):
if r.endswith(s):
return True

return False

s = "world"
t = "hello"
r = "helloworld"
print(solve(s, t, r))

## Input

"world", "hello", "helloworld"

## Output

True