Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #include <algorithm>
- #include <map>
- #include <string>
- using WordChars_t = std::unordered_map<char, uint8_t>;
- static WordChars_t parse_word(const std::string &in)
- {
- WordChars_t ret;
- std::string word;
- word.resize(in.size());
- std::transform(in.begin(), in.end(), word.begin(), [] (const char &c)
- {
- return std::tolower(c);
- });
- for (size_t i = 0; i < 26; i++)
- {
- size_t count = 0;
- char current = 'a' + i;
- for (const char c : word)
- {
- if (current == c)
- {
- count++;
- }
- }
- ret[current] = count;
- }
- return ret;
- }
- static bool find_anagrams(const std::vector<std::string> & dictionary, const std::string & word, std::vector<std::string> & anagrams)
- {
- auto parsedWord = parse_word(word);
- for (const auto &testPhrase : dictionary)
- {
- if (parsedWord != parse_word(testPhrase))
- continue;
- anagrams.push_back(testPhrase);
- }
- return anagrams.size() != 0;
- }
- int main()
- {
- std::vector<std::string> testPhrase = {"School master", "black", "cat", "Astronomer", "Dormitory"};
- std::vector<std::string> search = {"Dirty room", "Moon starer", "The classroom"};
- for (const auto &in : search)
- {
- std::cout << in << std::endl;
- std::vector<std::string> anagrams;
- if (find_anagrams(testPhrase, in, anagrams))
- {
- for (const auto &fml : anagrams)
- {
- std::cout << "\t" << fml << std::endl;
- }
- }
- else
- {
- std::cout << "\tno anagrams found" << std::endl;
- }
- }
- __debugbreak();
- }
Add Comment
Please, Sign In to add comment