1

Basically I have a problem with 2 subquestions. First question is: Given 2 strings, determine if they are anagrams. Second is a bit harder. You have N strings and have to determine if those are anagrams of each other.

I've solved the first one and I'll write the code below, but for the second one I have no idea. I was thinking it's possible to somehow do it by reading N strings from an array of strings, and then to use a for sequence to read each of them and compare them but I have no idea how to exactly.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    string word1; string word2;

    getline(cin,word1);
    getline(cin,word2);

    if(word1.length()==word2.length()){
        sort(word1.begin(), word1.end());
        sort(word2.begin(), word2.end());
    if(word1==word2) cout<<"The words are anagrams of each other"<<endl;
    else cout<<"The words are not anagrams of each other"<<endl;
    }
    else cout<<"The words are not the same length"<<endl;
return 0;
}
3
  • 1
    if string1 is an anagram of string2 and string3, string2 and string3 are definitely anagrams of each other Oct 17, 2013 at 17:36
  • Oh, yeah. Sorry for that. I got lost in all the stuff. Even so. I can't figure out how to read an array of strings. Everything I searched uses pointers, and I did not learn those yet. Oct 17, 2013 at 17:38
  • @DragoşPaulMarinescu sorting the strings to check is extremely inefficient and not necessary check my answer out
    – aaronman
    Oct 17, 2013 at 17:44

2 Answers 2

0

Finding if two strings are anagrams is extremely simple especially for the ASCII character set. The best approach is to create an int array of size 256. Go through the first string and for each char ++ that int. Do the same thing for the second string and check if the array's ended up the same.

To extend this to multiple string is easy since if

a is anagram of b and b is anagram of c then a is anagram of c

If you are doing this with a non ASCII character set that is larger it might be a good idea to use a hashmap instead of a bitset.

2
  • I'll try your solution later today. First I'd want to solve it, if possible based on what I already coded for the first solution. Would be nice to take on your solution as a "challenge". Oct 17, 2013 at 17:51
  • @DragoşPaulMarinescu IMO it's pretty simple, if you actually wrote your own sort your solution would actually require way more effort, I don't want to just give you the code but I did it in about 10 lines of code so it's not that hard
    – aaronman
    Oct 17, 2013 at 17:54
0

If X is anagram of Y and Z , then Y and Z are also anagrams

So, simply repeat your logic , the simplest approach :-

std::vector<std::string> words; //Use a vector
size_t i;
std::string word1,word2;
//Get words from standard input
std::copy(std::istream_iterator<std::string> (std::cin),
          std::istream_iterator<std::string>(),
          std::back_inserter(words));

word1=words[0]; //Check anagram with first word
sort(word1.begin(), word1.end());
for(i=1; i<words.size();i++)
{
    word2=words[i];
    sort(word2.begin(), word2.end());
    if(word2!=word1)
       break;
}

if(i==words.size())
    std::cout<<"All Anagrams !";
7
  • When I write std::vector<std::string> words; the compiler gives an error saying "namespace std has no member "vector" " I haven't really used vectors before so this is a whole new world for me. Sorry for coming across as a noob :( Oct 17, 2013 at 17:49
  • @DragoşPaulMarinescu You need to include proper headers See Here
    – P0W
    Oct 17, 2013 at 17:53
  • Woops, forgot to #include <vector> Oct 17, 2013 at 17:54
  • Tried to use the code in visual studio, yet I get this error: puu.sh/4Spdn.png Oct 17, 2013 at 17:57
  • @DragoşPaulMarinescu #include<string> And before you ask how to quit entering words from console, its Ctrl+Z or simply F6, these marks the end of stream.
    – P0W
    Oct 17, 2013 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.