Skip to content

Commit 415f42b

Browse files
committed
Add solution #1640
1 parent 1d3cbd8 commit 415f42b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,441 LeetCode solutions in JavaScript
1+
# 1,442 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1264,6 +1264,7 @@
12641264
1637|[Widest Vertical Area Between Two Points Containing No Points](./solutions/1637-widest-vertical-area-between-two-points-containing-no-points.js)|Easy|
12651265
1638|[Count Substrings That Differ by One Character](./solutions/1638-count-substrings-that-differ-by-one-character.js)|Medium|
12661266
1639|[Number of Ways to Form a Target String Given a Dictionary](./solutions/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js)|Hard|
1267+
1640|[Check Array Formation Through Concatenation](./solutions/1640-check-array-formation-through-concatenation.js)|Easy|
12671268
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12681269
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12691270
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 1640. Check Array Formation Through Concatenation
3+
* https://leetcode.com/problems/check-array-formation-through-concatenation/
4+
* Difficulty: Easy
5+
*
6+
* You are given an array of distinct integers arr and an array of integer arrays pieces,
7+
* where the integers in pieces are distinct. Your goal is to form arr by concatenating
8+
* the arrays in pieces in any order. However, you are not allowed to reorder the integers
9+
* in each array pieces[i].
10+
*
11+
* Return true if it is possible to form the array arr from pieces. Otherwise, return false.
12+
*/
13+
14+
/**
15+
* @param {number[]} arr
16+
* @param {number[][]} pieces
17+
* @return {boolean}
18+
*/
19+
var canFormArray = function(arr, pieces) {
20+
const numberToPiece = new Map();
21+
for (const piece of pieces) {
22+
numberToPiece.set(piece[0], piece);
23+
}
24+
25+
let index = 0;
26+
while (index < arr.length) {
27+
const currentNumber = arr[index];
28+
if (!numberToPiece.has(currentNumber)) {
29+
return false;
30+
}
31+
const piece = numberToPiece.get(currentNumber);
32+
for (const num of piece) {
33+
if (arr[index] !== num) {
34+
return false;
35+
}
36+
index++;
37+
}
38+
}
39+
40+
return true;
41+
};

0 commit comments

Comments
 (0)