Skip to content

Commit 9931eb0

Browse files
authored
Update 1640-check-array-formation-through-concatenation.js
1 parent 11716db commit 9931eb0

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

1640-check-array-formation-through-concatenation.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
* @return {boolean}
55
*/
66
const canFormArray = function(arr, pieces) {
7-
const str = arr.join('-')
8-
for(let i = 0, len = pieces.length; i < len; i++) {
9-
const tmp = pieces[i].join('-')
10-
if(str.indexOf(tmp) === -1) return false
7+
const m = new Map()
8+
for(let i = 0, len = arr.length; i < len; i++) {
9+
m.set(arr[i], i)
10+
}
11+
for(let p of pieces) {
12+
let idx = m.get(p[0])
13+
console.log(idx)
14+
if(idx == null) return false
15+
for(let i = 1, len = p.length; i < len; i++) {
16+
console.log(m.has(p[i]))
17+
if(!m.has(p[i]) || arr[++idx] !== p[i]) return false
18+
}
1119
}
1220
return true
1321
};

0 commit comments

Comments
 (0)