/*
 * Name: Owen Sullivan
 * Date Submitted: 10/17/22
 * Assignment Name: Single-Word Anagram Finder
*/

#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>

using namespace std;

vector<string> loadWordlist(string filename);
vector<string> anagram(string word, vector<string> wordlist);

// int main() {
    
//     vector<string> words;
//     vector<string> anagrams;
//     string inputWord;

//     words = loadWordlist("wordlist.txt");

//     cout << "Find single-word anagrams for the following word: ";
//     cin >> inputWord;

//     anagrams = anagram(inputWord, words);

//     for (int i = 0; i < anagrams.size(); i++) {
        
//         cout << anagrams[i] << endl;
    
//     }

//     return 0;

// }

vector<string> loadWordlist(string filename) {
    
    vector<string> words;
    ifstream inFile;
    string word;

    inFile.open(filename);
    
    if (inFile.is_open()) {
        
        while (getline(inFile,word)) {
            
            if (word.length() > 0) {
                
                words.push_back(word);
            
            }
        
        }
        
        inFile.close();
    
    }
    
    return words;

}

vector<string> anagram(string word, vector<string> wordlist) {

    // Create vector to store anagrams as they're found
    vector<string> anagrams;

    // Loop through each word in the word list
    for (string member_w : wordlist) {
    
        // Create an unordered map with characters as keys and integers
        // as values (to store the number of words in the input letter)
        unordered_map<char, int> umap;

        // Only bother checking for an anagram if the current word in the
        // word list is the same length as the input word
        if (member_w.size() == word.size()) {

            // Loop through the characters in the input word
            for (char member_c : word) {

                // Increment the value at the key in the unordered map
                // that matches the current character
                umap[member_c]++;

            }

            // Loop through the characters in the current word in the
            // word list
            for (char member_c : member_w) {

                // If the current character exists as a key in the
                // unordered map (using find() against end() because
                // it iterates until a match is found or end())
                if (umap.find(member_c) != umap.end()) {

                    // Decrement the value at the key in the
                    // unordered map that matches the current character
                    umap[member_c]--;

                    // If the value at the key in the unordered map is
                    // equal to zero, delete it so that it can't ever
                    // go negative
                    if (umap[member_c] == 0) {

                        umap.erase(member_c);

                    }

                  // If the current character does not exist as a key
                  // in the unordered map, stop looping through characters
                  // because the word cannot possibly be an anagram without
                  // matching letters
                } else {

                    break;

                }

            }

            // If the unordered map is empty (meaning all letters have
            // been accounted for), add the word in the word list to the
            // anagrams vector
            if (umap.empty()) {

                anagrams.push_back(member_w);

            }

        }

    }

    // Return the filled anagrams vector
    return anagrams;

}