Compare Strings - Company-specific OAs / Google OA

https://algo.monster/problems/google_oa_compare_strings

For this problem, I preferred to convert each string I was comparing to a hash map of characters to count the occurrence of each letter. Since std::map is ordered, by comparing the occurrences of the first letter in either map, I was able to see if a string was strictly smaller very easily. Here’s my solution. Note I have a helper function to convert a string into a std::map.

#include // copy
#include // cin, cout
#include // back_inserter, istream_iterator, ostream_iterator, prev
#include // istringstream
#include // getline, string
#include // vector
#include

std::map<char, int> strToMap(const std::string inStr) {
std::map<char, int> map;
for(const char c : inStr) {
if(map.count(c) == 0) {
map[c] = 1;
}
else {
map[c]++;
}
}
return map;
}

std::vector compare_strings(std::vectorstd::string str1, std::vectorstd::string str2) {
std::vector output;

for(const std::string str2_current : str2) {
    std::map<char, int> str2Map = strToMap(str2_current);
    
    int numSmaller = 0;
    for(const std::string str1_current : str1) {
        std::map<char, int> str1Map = strToMap(str1_current);
        
        if(str1Map.begin()->second < str2Map.begin()->second) {
            numSmaller++;   
        }
    }
    output.push_back(numSmaller);
}

return output;

}