Ctrl K

Assemble From Fragments

Easy
You get an array target of distinct numbers and a collection fragments of short arrays. Taken together, the numbers across all fragments are distinct as well, and the fragment lengths add up to the length of target. You may lay the fragments end to end in any order, but you may not rearrange the numbers inside a fragment. Return True if this can produce exactly target, and False otherwise.
Example 1
target=[7, 3, 9, 1], fragments=[[9, 1], [7, 3]] → True

Put [7, 3] first and then [9, 1].

Example 2
target=[5, 6, 2], fragments=[[6, 5], [2]] → False

The numbers match, but the order inside [6, 5] is fixed, while the target needs 5 before 6.

Example 3
target=[4, 8], fragments=[[4], [3]] → False
Constraints
  • 1 ≤ len(fragments) ≤ len(target) ≤ 100
  • 1 ≤ len(fragments[i])
  • The fragment lengths sum to len(target)
  • 1 ≤ target[i], fragments[i][j] ≤ 100
  • Numbers in target are distinct, and so are all numbers across the fragments
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyRepeat Within Reach EasyThe Lost Ticket Easy
loading Python…
Input
target=[7, 3, 9, 1], fragments=[[9, 1], [7, 3]]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 91 tests on the server, hidden ones included.