|
4 | 4 |
|
5 | 5 | ## Description
|
6 | 6 |
|
7 |
| -<p>Check whether the original sequence <code>org</code> can be uniquely reconstructed from the sequences in <code>seqs</code>. The <code>org</code> sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 10<sup>4</sup>. Reconstruction means building a shortest common supersequence of the sequences in <code>seqs</code> (i.e., a shortest sequence so that all sequences in <code>seqs</code> are subsequences of it). Determine whether there is only one sequence that can be reconstructed from <code>seqs</code> and it is the <code>org</code> sequence.</p> |
| 7 | +<p>You are given an integer array <code>nums</code> of length <code>n</code> where <code>nums</code> is a permutation of the integers in the range <code>[1, n]</code>. You are also given a 2D integer array <code>sequences</code> where <code>sequences[i]</code> is a subsequence of <code>nums</code>.</p> |
| 8 | + |
| 9 | +<p>Check if <code>nums</code> is the shortest possible and the only <strong>supersequence</strong>. The shortest <strong>supersequence</strong> is a sequence <strong>with the shortest length</strong> and has all <code>sequences[i]</code> as subsequences. There could be multiple valid <strong>supersequences</strong> for the given array <code>sequences</code>.</p> |
| 10 | + |
| 11 | +<ul> |
| 12 | + <li>For example, for <code>sequences = [[1,2],[1,3]]</code>, there are two shortest <strong>supersequences</strong>, <code>[1,2,3]</code> and <code>[1,3,2]</code>.</li> |
| 13 | + <li>While for <code>sequences = [[1,2],[1,3],[1,2,3]]</code>, the only shortest <strong>supersequence</strong> possible is <code>[1,2,3]</code>. <code>[1,2,3,4]</code> is a possible supersequence but not the shortest.</li> |
| 14 | +</ul> |
| 15 | + |
| 16 | +<p>Return <code>true</code><em> if </em><code>nums</code><em> is the only shortest <strong>supersequence</strong> for </em><code>sequences</code><em>, or </em><code>false</code><em> otherwise</em>.</p> |
| 17 | + |
| 18 | +<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> |
8 | 19 |
|
9 | 20 | <p> </p>
|
10 | 21 | <p><strong>Example 1:</strong></p>
|
11 | 22 |
|
12 | 23 | <pre>
|
13 |
| -<strong>Input:</strong> org = [1,2,3], seqs = [[1,2],[1,3]] |
| 24 | +<strong>Input:</strong> nums = [1,2,3], sequences = [[1,2],[1,3]] |
14 | 25 | <strong>Output:</strong> false
|
15 |
| -<strong>Explanation:</strong> [1,2,3] is not the only one sequence that can be reconstructed, because [1,3,2] is also a valid sequence that can be reconstructed. |
| 26 | +<strong>Explanation:</strong> There are two possible supersequences: [1,2,3] and [1,3,2]. |
| 27 | +The sequence [1,2] is a subsequence of both: [<strong><u>1</u></strong>,<strong><u>2</u></strong>,3] and [<strong><u>1</u></strong>,3,<strong><u>2</u></strong>]. |
| 28 | +The sequence [1,3] is a subsequence of both: [<strong><u>1</u></strong>,2,<strong><u>3</u></strong>] and [<strong><u>1</u></strong>,<strong><u>3</u></strong>,2]. |
| 29 | +Since nums is not the only shortest supersequence, we return false. |
16 | 30 | </pre>
|
17 | 31 |
|
18 | 32 | <p><strong>Example 2:</strong></p>
|
19 | 33 |
|
20 | 34 | <pre>
|
21 |
| -<strong>Input:</strong> org = [1,2,3], seqs = [[1,2]] |
| 35 | +<strong>Input:</strong> nums = [1,2,3], sequences = [[1,2]] |
22 | 36 | <strong>Output:</strong> false
|
23 |
| -<strong>Explanation:</strong> The reconstructed sequence can only be [1,2]. |
| 37 | +<strong>Explanation:</strong> The shortest possible supersequence is [1,2]. |
| 38 | +The sequence [1,2] is a subsequence of it: [<strong><u>1</u></strong>,<strong><u>2</u></strong>]. |
| 39 | +Since nums is not the shortest supersequence, we return false. |
24 | 40 | </pre>
|
25 | 41 |
|
26 | 42 | <p><strong>Example 3:</strong></p>
|
27 | 43 |
|
28 | 44 | <pre>
|
29 |
| -<strong>Input:</strong> org = [1,2,3], seqs = [[1,2],[1,3],[2,3]] |
30 |
| -<strong>Output:</strong> true |
31 |
| -<strong>Explanation:</strong> The sequences [1,2], [1,3], and [2,3] can uniquely reconstruct the original sequence [1,2,3]. |
32 |
| -</pre> |
33 |
| - |
34 |
| -<p><strong>Example 4:</strong></p> |
35 |
| - |
36 |
| -<pre> |
37 |
| -<strong>Input:</strong> org = [4,1,5,2,6,3], seqs = [[5,2,6,3],[4,1,5,2]] |
| 45 | +<strong>Input:</strong> nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]] |
38 | 46 | <strong>Output:</strong> true
|
| 47 | +<strong>Explanation:</strong> The shortest possible supersequence is [1,2,3]. |
| 48 | +The sequence [1,2] is a subsequence of it: [<strong><u>1</u></strong>,<strong><u>2</u></strong>,3]. |
| 49 | +The sequence [1,3] is a subsequence of it: [<strong><u>1</u></strong>,2,<strong><u>3</u></strong>]. |
| 50 | +The sequence [2,3] is a subsequence of it: [1,<strong><u>2</u></strong>,<strong><u>3</u></strong>]. |
| 51 | +Since nums is the only shortest supersequence, we return true. |
39 | 52 | </pre>
|
40 | 53 |
|
41 | 54 | <p> </p>
|
42 | 55 | <p><strong>Constraints:</strong></p>
|
43 | 56 |
|
44 | 57 | <ul>
|
45 |
| - <li><code>1 <= n <= 10^4</code></li> |
46 |
| - <li><code>org</code> is a permutation of {1,2,...,n}.</li> |
47 |
| - <li><code>1 <= segs[i].length <= 10^5</code></li> |
48 |
| - <li><code>seqs[i][j]</code> fits in a 32-bit signed integer.</li> |
| 58 | + <li><code>n == nums.length</code></li> |
| 59 | + <li><code>1 <= n <= 10<sup>4</sup></code></li> |
| 60 | + <li><code>nums</code> is a permutation of all the integers in the range <code>[1, n]</code>.</li> |
| 61 | + <li><code>1 <= sequences.length <= 10<sup>4</sup></code></li> |
| 62 | + <li><code>1 <= sequences[i].length <= 10<sup>4</sup></code></li> |
| 63 | + <li><code>1 <= sum(sequences[i].length) <= 10<sup>5</sup></code></li> |
| 64 | + <li><code>1 <= sequences[i][j] <= n</code></li> |
| 65 | + <li>All the arrays of <code>sequences</code> are <strong>unique</strong>.</li> |
| 66 | + <li><code>sequences[i]</code> is a subsequence of <code>nums</code>.</li> |
49 | 67 | </ul>
|
50 | 68 |
|
51 |
| -<p> </p> |
52 |
| - |
53 |
| -<p><b><font color="red">UPDATE (2017/1/8):</font></b><br /> |
54 |
| -The <i>seqs</i> parameter had been changed to a list of list of strings (instead of a 2d array of strings). Please reload the code definition to get the latest changes.</p> |
55 |
| - |
56 | 69 | ## Solutions
|
57 | 70 |
|
58 | 71 | <!-- tabs:start -->
|
|
0 commit comments