I am trying to learn Python. Consider this simple anagram checker in C:
bool are_anagrams(const char* str1, const char* str2)
{
int str1_count[NUM_CHARS] = {0};
int str2_count[NUM_CHARS] = {0};
for(int i = 0; i < strlen(str1); i++)
{
str1_count[str1[i] - 'a']++;
}
for(int i = 0; i < strlen(str2); i++)
{
str2_count[str2[i] - 'a']++;
}
for(int i = 0; i < NUM_CHARS; i++)
{
if(str1_count[i] != str2_count[i])
{ return false; }
}
return true;
}
Specifically, how is the line str1_count[str2[i] - 'a']++ done in Python?
Or even better,
.
Edit: in response to @gnibbler:
Here’s a quick run-time comparison, where x axis gives string length; blue is your function, green is mine. Both look pretty linear to me.