Created
September 4, 2012 18:49
-
-
Save mueschm/3624918 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
//Check to see if the strings contain the same number of characters | |
//For time purposes we assume that the strings only contain capital letters | |
bool hasSameCharacters(String string1, String string2) | |
{ | |
//Get the size of the strings once | |
int string1Size = string1.size(); | |
int string2Size = string2.size(); | |
//If the sizes are not equal than they | |
//do not contain the same characters | |
if(string1Size != String2Size) | |
return false; | |
//Create a hash table to hold the amount of chars in the string | |
int hashTable[26] = {0}; | |
//Initalize i onto the stack once | |
int i; | |
//Go through the first string and hash all the characters | |
for(i = 0;i < string1Size;i++) | |
hashTable[string1[i]-65]++; | |
//Go through the hash table with the second strings characters | |
//if the hash table at any point contains a -1 then the character either | |
//appeared more in the second string or was not in the first string | |
for(i = 0;i < stringSize2;i++) | |
if(--hashTable[string2[i]-65] == -1) | |
return false; | |
//Since we know the sizes were the same due to our first check we know that | |
//if the two were not equal it would have failed the previous test | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment