Skip to content

Commit

Permalink
Create 2227.Encrypt-and-Decrypt-Strings.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Apr 10, 2022
1 parent f3b3b14 commit 34014e9
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Encrypter {
unordered_map<char, string>Map1;
unordered_map<string, vector<char>>Map2;
unordered_map<string, int>count;

public:
Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary)
{

for (int i=0; i<keys.size(); i++)
{
Map1[keys[i]] = values[i];
Map2[values[i]].push_back(keys[i]);
}

for (auto s: dictionary)
{
string t = encrypt(s);
if (t!="")
count[t]++;
}
}

string encrypt(string word1)
{
string ret;
for (char ch: word1)
{
if (Map1.find(ch)==Map1.end())
return "";
ret += Map1[ch];
}

return ret;
}

int decrypt(string word2)
{
if (count.find(word2)==count.end())
return 0;
else
return count[word2];
}
};

/**
* Your Encrypter object will be instantiated and called as such:
* Encrypter* obj = new Encrypter(keys, values, dictionary);
* string param_1 = obj->encrypt(word1);
* int param_2 = obj->decrypt(word2);
*/

0 comments on commit 34014e9

Please sign in to comment.