The OP posted no such restriction. But it would be simple enough to first check that the word length is the same as the number of letters to check, check for invalid letters, then check that each letter appears only once. That would be slower, but it would be the optimal solution.
One way to implement that would indeed be to use count().
:-)
[edit] He could use a set, but it would be a lot more overhead to do the same thing.
After that when I make every possible compination I use:
1 2
if( !words[str].empty() )
cout << str << " is a possible word\n";
I think that this was a good reason to use maps. They make the searching through their contents using a keyword much faster than just searching a vector, and since the keyword is the same as the word, if it exists I have a word.
[EDIT]
I started reading about sets now and you are probably right. I didn't know about it. Thanx.
I brought up permutations because the words he is looking for are up to 7 letters long, so there will be no problem generating these very quickly... if the words were up to 28 letters long I wouldn't have suggested it.
I wasn't attacking you. The reason I suggest against mixing letters and looking for valid words was demonstrated quite nicely by my extreme example. (There is only one word that is 28 letters long. :-)
For 7-letter words it would be 823,543 permutations, which is more than four times as many words in the OWL.
By only looking for known words, we avoid the permutations altogether and obviate the lookup step to see if we have a valid word. That is a single scan over the word list, and a single scan over each word for invalid letters. That would take significantly less time.
Using Duoas solution with the find_first_not_of() and the count() functions I came up with the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//words is a vector that holds all the words from a dictionary file
//letters is a string that holds the letters the user enters
//the bool "asymfonia" is to compare how many same letters the word from
//the dictionary and the letters of the user have (for example if the letters
//from the user are "bot" then the "boot" will be illegal, but "bot" from letters boot is legal).
for( vector<string>::iterator it = words.begin(); it != words.end(); ++it ){
size_t found = it->find_first_not_of( letters );
if( found == string::npos ){
bool asymfonia = false;
for( string::iterator sit = it->begin(); sit != it->end(); ++sit )
if( count( letters.begin(), letters.end(), *sit ) < count( it->begin(), it->end(), *sit ) )
asymfonia = true;
if( !asymfonia )
cout << "Found word: " << *it << '\n';
//keyWords( *it ); //I originally send my word to a function
}
}