wo arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.
Given two arrays a
and b
, check whether they are similar.
Example
For
a = [1, 2, 3]
andb = [1, 2, 3]
, the output should beareSimilar(a, b) = true
.The arrays are equal, no need to swap any elements.
For
a = [1, 2, 3]
andb = [2, 1, 3]
, the output should beareSimilar(a, b) = true
.We can obtain
b
froma
by swapping2
and1
inb
.For
a = [1, 2, 2]
andb = [2, 1, 1]
, the output should beareSimilar(a, b) = false
.Any swap of any two elements either in
a
or inb
won't makea
andb
equal.
Code
function areSimilar(a, b) {
}
'알고리즘(CF)' 카테고리의 다른 글
18. palindromeRearranging (1) | 2017.06.17 |
---|---|
17. arrayChange (1) | 2017.06.17 |
15. addBorder (1) | 2017.06.17 |
14. alternatingSums (1) | 2017.06.17 |
13. reverseParentheses (1) | 2017.06.17 |