Merge Ordered Message Fragments
Updated
Problem statement
A message was split into fragments. You are given the fragments in their original order as a string array message. Rebuild the message by merging the fragments from left to right.
Start with message[0] as the text. Then, for each next fragment in order, find its overlap with the text: the largest integer k such that
0 <= k <= min(text.length, fragment.length), and- the last
kcharacters of the text equal the firstkcharacters of the fragment.
Those k characters are written only once: append the fragment without its first k characters to the text.
- Always use the longest overlap, not a shorter one.
- The overlap is with the whole text built so far, not only with the previous fragment.
- The overlap may be a whole fragment. If the text already ends with the fragment, nothing is added.
- A fragment that appears inside the text but not at its end is not merged into it; only an overlap at the end of the text counts.
Return the rebuilt message. It starts with 'A' and ends with 'Z'.
Function
mergeFragments(message: String[]) → StringExamples
Example 1
message = ["Aghb","bfbcb","b===Z"]return = "Aghbfbcb===Z"The text starts as "Aghb". It ends with "b" and "bfbcb" starts with "b"; the longer ends "hb", "ghb" and "Aghb" do not match its start, so the overlap is 1 and the text becomes "Aghbfbcb". That ends with "b" and "b===Z" starts with it, so the overlap is 1 again and the result is "Aghbfbcb===Z".
Example 2
message = ["Aabab","ababZ"]return = "AababZ"Both "ab" (length 2) and "abab" (length 4) are at the end of "Aabab" and at the start of "ababZ". The longest one, length 4, is used, so only "Z" is added. Using the shorter overlap would give "AabababZ", which is wrong.
Example 3
message = ["Axyz","xy","y","Z"]return = "AxyzxyZ""xy" is inside "Axyz" but the text ends with "z", so the overlap is 0 and the text becomes "Axyzxy". That text already ends with "y", so the whole fragment "y" overlaps and nothing is added. "Z" has no overlap, so the result is "AxyzxyZ".
Constraints
1 <= message.length <= 10001 <= message[i].length <= 2000- The total length of all fragments is at most
3 * 10^4. - Every character is a printable ASCII character (codes
32to126, the space included). message[0]starts with'A', and the last fragment ends with'Z'.
More Veeam interview questions
Similar questions at other companies
- Longest Palindromic SubsequenceAXQ Capital · OA
- Classify Password StrengthHackerRank · OA · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
- Polynomial CoefficientsShopify · OAShopify interview questions
- Basic Calculator IITypeSafe AI · OA
- Count One-Swap Number PairsCapital One · OACapital One interview questions
- Find a String Within One Mismatch of AllBarclays · OABarclays interview questions
- Maximum String OperationsMicrosoft · OAMicrosoft interview questions
- Reliable LLM Call with Independent Retry BudgetsStripe · OAStripe interview questions
- Maximum Equal-Bit Pairs in a SubsequenceAmazon · OA
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions