Merge Two Ordered Sequences

Asked at GoogleOnsite

Reported by the problem's creator, not verified

You are given two integer arrays a and b. Within each array the order is a rule: an element must come before every element that appears after it in the same array. A value that appears in both arrays is the same item. Implement can_merge(a, b) to decide whether all distinct values can be laid out in one sequence, each value once, that keeps the order of a and the order of b.

Examples

  1. Example 1

    Input: a = [1,2,3], b = [2,4,5]

    Output: true

    Explanation: The order 1, 2, 3, 4, 5 keeps 1, 2, 3 in order and 2, 4, 5 in order.

  2. Example 2

    Input: a = [1,6,4], b = [4,1]

    Output: false

    Explanation: a needs 1 before 4 and b needs 4 before 1, so no single order works.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.