Skip to content

Instantly share code, notes, and snippets.

@hucancode
Last active September 14, 2022 02:21
Show Gist options
  • Save hucancode/db013b195912435ae826f4ecce1de2f5 to your computer and use it in GitHub Desktop.
Save hucancode/db013b195912435ae826f4ecce1de2f5 to your computer and use it in GitHub Desktop.
check if 2 sorted lists of string have any overlaping item in O(logn)
// precondition: a & b are both sorted
bool overlap(vector<string>& a, vector<string>& b) {
auto i = a.begin();
auto j = b.begin();
auto n = upper_bound(i, a.end(), *b.rbegin());
auto m = upper_bound(j, b.end(), *a.rbegin());
while(i < n && j < m) {
if(*i == *j) {
return true;
}
auto nextj = lower_bound(j, m, *i);
auto nexti = lower_bound(i, n, *j);
i = nexti;
j = nextj;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment