They say that they are optimized for time, so I think that we will safely abuse the space as much as we want.
In this case, you can perform an initial pass along 10,000-lines and build a mapping from each of the unique characters present in 10,000 to their index (and not to the set of their indices). So you can ask a question display that contains the character "x"? Call this mapping M> (order: O (nm) when n is the number of rows and m is their maximum length)
To optimize the time again, you can reduce the input string stdin to unique characters and put them in the Q queue. (Order O (p), p is the length of the input string)
Start a new disjoint set, say S. Then let S = Q.extractNextItem.
Now you can iterate over the remaining unique characters and find which sets contain all of them.
So far (Q is not empty) (loops O (p)) {
S = S intersects Q.extractNextItem (close to O (1) depending on your implementation of disjoint sets)
}
voila, return S.
Total time: O (mn + p + p * 1) = O (mn + p)
(Early in the morning here, I hope the time analysis was right)
source share