Check if 2 arrays are similar without hashing or sorting

后端 未结 4 1062
南旧
南旧 2021-01-13 10:20

We need to check if 2 arrays are similar or not. The elements can be duplicate as well. For example A = {2,3,4,5,6,6} and B = {3,6,2,4,6,5} are similar.

I have a nai

4条回答
  •  野的像风
    2021-01-13 10:47

    You can use a binary search tree that you build from one of them. Now go over the other one and check if the value is already in the binary search tree. This one runs in O(nlgn) and use O(n) space.

提交回复
热议问题