Skip to content

Instantly share code, notes, and snippets.

@andrewgho
Last active August 29, 2015 14:10
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 andrewgho/935ef44a1b75bf5d0152 to your computer and use it in GitHub Desktop.
Save andrewgho/935ef44a1b75bf5d0152 to your computer and use it in GitHub Desktop.
/* gcc -Wall -Wno-char-subscripts -o is_steal is_steal.c */
#include <stdio.h>
int is_steal(const char *base, const char *steal) {
char count[26] = {0};
int bl, sl;
for(bl = sl = 0; base[bl]; bl++) {
if(count[base[bl]] > 0) {
count[base[bl]]--;
} else {
for(; steal[sl] && steal[sl] != base[bl]; sl++, count[steal[sl]]++);
if(!steal[sl]) return 0;
}
}
for(; sl <= bl && steal[sl]; sl++);
return sl > bl;
}
int main(int argc, const char **argv) {
if(is_steal(argv[1], argv[2])) {
puts("true");
return 0;
} else {
puts("false");
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment