#include #include #include #include #include "HashTable.h" bool anagram(char *s, char *t) { // Keep a counter (hashtable), tally up the characters in s HashTable ht = HashTableNew(); for(int i=0; s[i] != '\0'; i++) { int num = HashTableGetOrDefault(ht, s[i], 0); num++; HashTableInsert(ht, s[i], num); } // Go through t, for each character, check it's in the counter with // a count > 0, then subtract 1 from that counter for(int i=0; t[i] != '\0'; i++) { int num = HashTableGetOrDefault(ht, t[i], 0); if (num == 0) { HashTableFree(ht); return false; } num--; if (num == 0) { HashTableDelete(ht, t[i]); } else { HashTableInsert(ht, t[i], num); } } // Check that the hashtable is empty at the end //return HashTableSize(ht) == 0; if (HashTableSize(ht) == 0) { HashTableFree(ht); return true; } else { HashTableFree(ht); return false; } } static HashTable createHashTable(char * s) { HashTable ht = HashTableNew(); for(int i=0; s[i] != '\0'; i++) { int num = HashTableGetOrDefault(ht, s[i], 0); num++; HashTableInsert(ht, s[i], num); } return ht; } bool anagram(char *s, char *t) { if (strlen(s) != strlen(t)) { return false; } HashTable hts = createHashTable(s); HashTable htt = createHashTable(t); for(int i=0; i