Is there an algorithm (preferably constant time) to check if set A is a subset of set B?
Creating the data structures to facilitate this problem does not count against the runtime.
Well, you're going to have to look at each element of A
, so it must be at least linear time in the size of A
.
An O(A+B)
algorithm is easy using hashtables (store elements of B
in a hashtable, then look up each element of A
). I don't think you can do any better unless you know some advance structure for B
. For instance, if B
is stored in sorted order, you can do O(A log B)
using binary search.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With