Skip to content

Instantly share code, notes, and snippets.

@goctave
Created April 19, 2012 02:40
Show Gist options
  • Save goctave/2417969 to your computer and use it in GitHub Desktop.
Save goctave/2417969 to your computer and use it in GitHub Desktop.
CareerCup_1.4@1point3acres
#include <iostream>
#include <string>
using namespace std;
bool is_anagram(const string &str1, const string &str2)
{
if(str1.size() != str2.size())
return false;
int cnt1[256] = {0};
int cnt2[256] = {0};
for(int i = 0; i < str1.size(); i++)
{
cnt1[str1[i]]++;
cnt2[str2[i]]++;
}
for(int i = 0; i < 256; i++)
if(cnt1[i] != cnt2[i])
return false;
return true;
}
int main()
{
cout << is_anagram("ababab", "aaabbb") << endl;
cout << is_anagram("aaaaaa", "bbbbbb") << endl;
cout << is_anagram("abcdef", "aeabcd") << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment