⭐️  [ Kt, Js, Py3, Cpp ] 🎯  Do we "have" what we "need"?
Solution #1: Map\n\nReturn true if and only if we can make what we need from what we have via a map m which stores the first value A[0] of each array "piece" A 
**Solution #1: Map**\n\nReturn `true` if and only if we can `make` what we `need` from what we `have` via a map `m` which stores the first value `A[0]` of each array "piece" `A` which we `have`.\n\n---\n\n*Kotlin*\n```\nclass Solution {\n    fun canFormArray(need: IntArray, have: Array<IntArray>): Boolean {\n        var m = mutableMapOf<Int, Int>()\n        var make = mutableListOf<Int>()\n        have.forEachIndexed { i, A -> m[A[0]] = i }\n        var i = 0\n        var N = need.size\n        while (i < N) {\n            if (!m.contains(need[i]))\n                return false\n            var j = m[need[i]]!!\n            make.addAll(have[j].toList())\n            i += have[j].size\n        }\n        return need.toList() == make\n    }\n}\n```\n\n*Javascript*\n```\nlet canFormArray = (need, have, m = new Map(), make = []) => {\n    have.forEach((A, i) => m.set(A[0], i));\n    let i = 0,\n        N = need.length;\n    while (i < N) {\n        if (!m.has(need[i]))\n            return false;\n        let j = m.get(need[i]);\n        make.push(...have[j]);\n        i += have[j].length;\n    }\n    return _.zip(need, make).every(pair => pair[0] == pair[1]);\n};\n```\n\n*Python3*\n```\nclass Solution:\n    def canFormArray(self, need: List[int], have: List[List[int]]) -> bool:\n        m = {A[0]: i for i, A in enumerate(have)}\n        make = []\n        i = 0\n        N = len(need)\n        while i < N:\n            if need[i] not in m:\n                return False\n            j = m[need[i]]\n            make.extend(have[j].copy())\n            i += len(have[j])\n        return need == make\n```\n\n*C++*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    using VVI = vector<VI>;\n    using Map = unordered_map<int, int>;\n    bool canFormArray(VI& need, VVI& have, Map m = {}, VI make = {}) {\n        for (auto i{ 0 }; i < have.size(); ++i)\n            m[have[i][0]] = i;\n        int i = 0,\n            N = need.size();\n        while (i < N) {\n            if (m.find(need[i]) == m.end())\n                return false;\n            auto j = m[need[i]];\n            make.insert(make.end(), have[j].begin(), have[j].end());\n            i += have[j].size();\n        }\n        return need == make;\n    }\n};\n```\n\n---\n\n**Solution #2: Queue**\n\nUse a queue `q` to store matching candidate pieces from what we `have` compared to what we `need`.  Return `true` if and only if we `have` what we `need`.\n\n---\n\n*Kotlin*\n```\nclass Solution {\n    fun canFormArray(need: IntArray, have: Array<IntArray>): Boolean {\n        var q: Queue<Int> = LinkedList<Int>()\n        for (i in 0 until need.size) {\n            var x = need[i]\n            if (0 < q.size) {\n                if (x != q.peek())\n                    return false\n                q.poll()\n                continue\n            }\n            var found = false\n            for (j in 0 until have.size) {\n                if (x == have[j][0]) {\n                    found = true\n                    for (k in 1 until have[j].size)\n                        q.add(have[j][k])\n                    break\n                }\n            }\n            if (!found)\n                return false\n        }\n        return true\n    }\n}\n```\n\n*Javascript*\n```\nlet canFormArray = (need, have, q = []) => {\n    for (let x of need) {\n        if (q.length) {\n            if (x != q[0])\n                return false;\n            q.shift();\n            continue;\n        }\n        let found = false;\n        for (let piece of have) {\n            if (x == piece[0]) {\n                q.push(...piece.slice(1, piece.length));\n                found = true;\n                break;\n            }\n        }\n        if (!found)\n            return false;\n    }\n    return true;\n};\n```\n\n*Python3*\n```\nclass Solution:\n    def canFormArray(self, need: List[int], have: List[List[int]]) -> bool:\n        q = deque()\n        for x in need:\n            if q:\n                if x != q[0]:\n                    return False\n                q.popleft()\n                continue\n            found = False\n            for piece in have:\n                if x == piece[0]:\n                    found = True\n                    q.extend(piece[1:])\n                    break\n            if not found:\n                return False\n        return True\n```\n\n*C++*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    using VVI = vector<VI>;\n    using Queue = queue<int>;\n    bool canFormArray(VI& need, VVI& have, Queue q = {}) {\n        for (auto x: need) {\n            if (q.size()) {\n                if (x != q.front())\n                    return false;\n                q.pop();\n                continue;\n            }\n            auto found{ false };\n            for (auto& piece: have) {\n                if (x == piece[0]) {\n                    found = true;\n                    for (auto i{ 1 }; i < piece.size(); q.push(piece[i++]));\n                    break;\n                }\n            }\n            if (!found)\n                return false;\n        }\n        return true;\n    }\n};\n```
[]