Valid Anagram
Note:
This code was written during a crunch period and isn't perfect. There will
be some errant spacing, some files will be
using namespace std,
etc. But it's all still usable and can be a
handy guideline if you're learning Data Structures.
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
#include <queue>
bool validAnagram(std::string wordA, std::string wordB)
{
std::map<char, int> wALetterCount;
std::map<char, int> wBLetterCount;
if (wordA.size() != wordB.size())
return false;
for (int i = 0; i < wordA.size(); i++)
{
wALetterCount[wordA[i]]++;
wBLetterCount[wordB[i]]++;
}
for (int i = 0; i < wordA.size(); i++)
{
if (wALetterCount[wordA[i]] != wBLetterCount[wordA[i]])
{
return false;
}
}
return true;
}
int main(int argc, char *argv[])
{
if (validAnagram("listen", "silent"))
{
std::cout << "Anagram" << std::endl;
}
else
{
std::cout << "Not Anagram" << std::endl;
}
if (validAnagram("hello", "goodbye"))
{
std::cout << "Anagram" << std::endl;
}
else
{
std::cout << "Not Anagram" << std::endl;
}
}