{"id":7592,"date":"2020-10-31T23:36:09","date_gmt":"2020-11-01T06:36:09","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7592"},"modified":"2020-11-01T00:19:19","modified_gmt":"2020-11-01T07:19:19","slug":"leetcode-1640-check-array-formation-through-concatenation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1640-check-array-formation-through-concatenation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1640. Check Array Formation Through Concatenation"},"content":{"rendered":"\n<p>You are given an array of&nbsp;<strong>distinct<\/strong>&nbsp;integers&nbsp;<code>arr<\/code>&nbsp;and an array of integer arrays&nbsp;<code>pieces<\/code>, where the integers in&nbsp;<code>pieces<\/code>&nbsp;are&nbsp;<strong>distinct<\/strong>. Your goal is to form&nbsp;<code>arr<\/code>&nbsp;by concatenating the arrays in&nbsp;<code>pieces<\/code>&nbsp;<strong>in any order<\/strong>. However, you are&nbsp;<strong>not<\/strong>&nbsp;allowed to reorder the integers in each array&nbsp;<code>pieces[i]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if it is possible&nbsp;<\/em><em>to form the array&nbsp;<\/em><code>arr<\/code><em>&nbsp;from&nbsp;<\/em><code>pieces<\/code>. Otherwise, return&nbsp;<code>false<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [85], pieces = [[85]]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [15,88], pieces = [[88],[15]]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Concatenate <code>[15]<\/code> then <code>[88]<\/code>\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [49,18,16], pieces = [[16,18,49]]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> Even though the numbers match, we cannot reorder pieces[0].\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [91,4,64,78], pieces = [[78],[4,64],[91]]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Concatenate <code>[91]<\/code> then <code>[4,64]<\/code> then <code>[78]<\/code><\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,3,5,7], pieces = [[2,4,6,8]]\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= pieces.length &lt;= arr.length &lt;= 100<\/code><\/li><li><code>sum(pieces[i].length) == arr.length<\/code><\/li><li><code>1 &lt;= pieces[i].length &lt;= arr.length<\/code><\/li><li><code>1 &lt;= arr[i], pieces[i][j] &lt;= 100<\/code><\/li><li>The integers in&nbsp;<code>arr<\/code>&nbsp;are&nbsp;<strong>distinct<\/strong>.<\/li><li>The integers in&nbsp;<code>pieces<\/code>&nbsp;are&nbsp;<strong>distinct<\/strong>&nbsp;(i.e., If we flatten pieces in a 1D array, all the integers in this array are distinct).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Store the index of the first number of each piece, for each number a in arr, concat the entire piece array whose first element equals to a.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {\n    unordered_map<int, int> m;\n    for (int i = 0; i < pieces.size(); ++i)\n      m[pieces[i][0]] = i;\n    \n    vector<int> ans;\n    for (int a : arr) {\n      if (!m.count(a)) continue;\n      ans.insert(end(ans), begin(pieces[m[a]]), end(pieces[m[a]]));\n    }\n    \n    return ans == arr;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of&nbsp;distinct&nbsp;integers&nbsp;arr&nbsp;and an array of integer arrays&nbsp;pieces, where the integers in&nbsp;pieces&nbsp;are&nbsp;distinct. Your goal is to form&nbsp;arr&nbsp;by concatenating the arrays in&nbsp;pieces&nbsp;in any&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[20,222,82],"class_list":["post-7592","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-array","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7592"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7592\/revisions"}],"predecessor-version":[{"id":7594,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7592\/revisions\/7594"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}