I am trying to change the characters in a string to numbers by looking at a 2D vector. Say for example I have "JOHN" then I will have to look at the table solution = [['J', '2'], ['O', 1], ['H', 3], ['N', 4]] then I need to change it to 2134. Here is what I have tried.
#include <iostream>
#include <unordered_map>
#include <vector>
bool isCryptSolution(std::vector<std::string> crypt, std::vector<std::vector<char>> solution)
{
// store the letters in an unordered_map for fast look up
std::unordered_map<char, char> mymap;
for (auto& elem: solution) {
mymap.insert({elem[0], elem[1]});
}
// iterate through each element of crypt and replace the characters with numbers
// and then turn the string to numbers
for (auto& str: crypt) {
for (auto& c: str) {
str.replace(str.find(c), 1, std::string(1, mymap.find(c)->second));
}
if (str.find('0') == 0) return false;
std::stoi(str);
}
return ( crypt.at(0) + crypt.at(1) == crypt.at(2));
}
Aucun commentaire:
Enregistrer un commentaire