Skip to content

Commit 5250cc0

Browse files
authored
Create 1460-make-two-arrays-equal-by-reversing-sub-arrays.js
1 parent 2817bc8 commit 5250cc0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} target
3+
* @param {number[]} arr
4+
* @return {boolean}
5+
*/
6+
const canBeEqual = function(target, arr) {
7+
if(target.length !== arr.length) return false
8+
const tHash = {}, aHash = {}
9+
for(let i = 0, len = arr.length; i < len;i++) {
10+
const t = target[i], a = arr[i]
11+
if(tHash[t] == null) tHash[t] = 0
12+
if(aHash[a] == null) aHash[a] = 0
13+
tHash[t]++
14+
aHash[a]++
15+
}
16+
17+
const keys = Object.keys(tHash)
18+
for(let k of keys) {
19+
if(tHash[k] !== aHash[k]) return false
20+
}
21+
22+
return true
23+
};

0 commit comments

Comments
 (0)