Created
November 20, 2010 05:20
-
-
Save jdoliner/707639 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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