Skip to main content

Merge Suffix Prefix

easy
Software engineer

Given two strings str1 and str2, implement a function that concatenates them by merging the longest common substring that appears as a suffix of str1 and a prefix of str2 into a single occurrence.

If there is no overlapping common substring, simply concatenate the two strings normally.

Example 1

Input

str1 = "hello", str2 = "loworld"

Output

"helloworld"

Explanation

The suffix of "hello" is "lo" and the prefix of "loworld" is "lo". This is the longest overlap. Instead of "helloloworld", we merge the "lo" to get "helloworld".

Example 2

Input

str1 = "abc", str2 = "abcdef"

Output

"abcdef"

Explanation

The entire string "abc" is a suffix of str1 and a prefix of str2.

Example 3

Input

str1 = "abc", str2 = "xyz"

Output

"abcxyz"

Explanation

There is no overlapping suffix/prefix, so they are concatenated normally.

Constraints

  • 0 <= str1.length, str2.length <= 10⁵
  • str1 and str2 consist only of lowercase English letters.

ScreeningOnsite

Rolling HashSimulationStringString MatchingTwo Pointers
Language
Code editor loads in the browser.

Output

Input

"hello"
"loworld"

Expected

"helloworld"

Your output

Run to see your output.