Skip to content

Instantly share code, notes, and snippets.

@anowell
Created October 23, 2012 07:45
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 anowell/3937503 to your computer and use it in GitHub Desktop.
Save anowell/3937503 to your computer and use it in GitHub Desktop.
hasSameCharacters with minor optimization for short strings
// Expected behavior:
// 'abc', 'cba' => true
// 'abca', 'cba' => true
// 'cba', 'abca' => true
// '', '' => true
// 'abcd', 'abc' => false
// 'abc', 'abcd' => false
bool hasSameCharacters(char* c1, char* c2)
{
// returning false was how we decided to handle invalid input
if(c1==NULL || c2==NULL) {
return false;
}
// char has 256 values, so we track characters found in these arrays
bool bc1[256] = {0};
bool bc2[256] = {0};
/*
* Short String Optimization: rather than compare bc1 and bc2 at end, we do 2 things:
* (SSO-1) Return as soon as we find char in c2 that was not in c1
* (SSO-2) Count and compare unique characters in each string at the end
*
* Idea: If c1 and c2 have the same number of unique characters
* and every char in c2 is also in c1,
* then we can conclude that every char in c1 is also in c2.
*/
int uniquec1 = 0;
int uniquec2 = 0;
for(int i=0; c1[i] != '\0'; ++i) {
int offset = c1[i];
// Found unique char in c1
if( bc1[offset] == false ) {
uniquec1++;
bc1[offset] = true;
}
}
for(int i=0; c2[i] != '\0'; ++i) {
int offset = c2[i];
// (SSO-1): No point in continuing if char was not in c1
if( bc1[offset] == false ) {
return false;
}
// Found unique char in c2
if( bc2[offset] == false ) {
uniquec2++;
bc2[offset] = true;
}
}
// (SSO-2): Compare unique character counts
return (uniquec1 == uniquec2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment