-
Notifications
You must be signed in to change notification settings - Fork 0
/
Are Similar.js
62 lines (43 loc) · 1.36 KB
/
Are Similar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements.
// Given two arrays, check whether they are similar.
// Example
// For A = [1, 2, 3] and B = [1, 2, 3], the output should be true;
// For A = [1, 2, 3] and B = [2, 1, 3], the output should be true;
// For A = [1, 2, 2] and B = [2, 1, 1], the output should be false.
// Input/Output
// [input] integer array A
// Array of integers.
// Constraints: 3 ≤ A.length ≤ 10000, 1 ≤ A[i] ≤ 1000.
// [input] integer array B
// Array of integers of the same length as A.
// Constraints: B.length = A.length, 1 ≤ B[i] ≤ 1000.
// [output] a boolean value
// true if A and B are similar, false otherwise.
function areSimilar(a, b) {
if (a.length !== b.length) {
return false;
}
let diff = 0;
let a1 = -1;
let a2 = -1;
let b1 = -1;
let b2 = -1;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
diff++;
if (diff > 2) {
return false;
}
if (a1 === -1) {
a1 = i;
b1 = i;
} else if (a2 === -1) {
a2 = i;
b2 = i;
} else {
return false;
}
}
}
return diff === 0 || (diff === 2 && a[a1] === b[b2] && a[a2] === b[b1]);
}