Possible Duplicate:
Check if array B is a permutation of A
Given 2 unsorted integer arrays a
and b
of equal size. Determine if b
is a permutation of a
. Can this be done in O(n) time
and O(1) space
?
The first solution that came to my mind is that is using XOR
, i.e. XOR all the elements of a and b and if the resultant is 0 which means that b is a permutation of a
. But he gives examples where this approach fails. For e.g -
a: [1 6 0 0 4] -- b: [1 0 6 1 5]
a: [1 6 0 0 5] -- b: [1 0 6 1 4]
Any one having any idea, that how to do it in O(n) time
and O(1) space
?