one way to do it is for each permutation p of WordToUnscramble, check if p is in dictionary. dictionary is just a std::set<std::string>.
another faster way is use std::map<std::string, std::vector<std::string>> as dictionary. When loading dictionary, sort each word alphabetically, ex. "clear" would become "acelr", then insert it into your dictionary, use the alphabetical word as key and insert the list of words as value. When looking WordToUnscramble, sort it alphabetically, then just return dictionary[alphabeticallyWordToUnscramble];. (you may want to check if it exists in dictionary before returning it)
5. Sort the letters before searching: std::sort( letters.begin(), letters.end() );
6. check if the word is in the database. if true, print. then shuffle the letters for the next permutation:
1 2 3 4 5 6 7
for (std::vector<std::string>::iterator it = m_dictionary.begin();
it != m_dictionary.end(); ++it)
if (*it == letters)
std::cout << letters << std::endl;
} while ( std::next_permutation( letters.begin(), letters.end() ) );
Note that there are MUCH faster ways to search for a word, but I iterate through the entire database on every permutation for simplicity here.
So if you simply hash the unscrambled string, you can easily find it in a database by either straight linear search where you try to find a string that matches the hashed value of the unscrambled string (this will be O(N)). Or you can already have the strings in your database already be in a hashmap then lookup is reduced to O(1).
Can you guys help me start simply with a program that simply counts the amount of letters in a word?
ie I type in 'recal'
it'll count 5 letters?
also, if it can count how many of each letters there are
ie I type in recaler - it has 2 Rs, 2 Es, and 1 of C, A and L, how do I get my program to get that information?