Skip to content

Instantly share code, notes, and snippets.

@jdoliner
Created November 20, 2010 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdoliner/707639 to your computer and use it in GitHub Desktop.
Save jdoliner/707639 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#define mask(c) (1 << ('z' - c))
#define all (1 << (('z' - 'a') + 2)) - 1//bitmap when all the characters have been seen
int contains(char *haystack, char *needle) {
int seen = 0; //bitmap of characters we have been seen
for(;*haystack;haystack++) {
if (seen == all)
return 1;
seen |= mask(*haystack);
}
for(;*needle; needle++)
if (!(mask(*needle) & seen))
return 0;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment