# Duplicate Encoder [6 kyu] [Duplicate Encoder](https://www.codewars.com/kata/54b42f9314d9229fd6000d9c) 6 kyu ## Solution ```cpp= /** *** Author: R-CO *** E-Mail: daniel1820kobe@gmail.com *** Date: 2020-07-21 **/ #include <cctype> #include <cstdlib> #include <iostream> using std::cout; #include <unordered_map> using std::unordered_map; #include <string> using std::string; struct TestCaseStruct { string input; string expected_output; }; using RefMap = unordered_map<char, size_t>; class DuplicateEncoder { public: string duplicate_encoder(const string &word) { const auto input_size = word.size(); string output(input_size, '('); RefMap ref_map; for (size_t index = 0; index < input_size; ++index) { const auto ch = static_cast<char>(tolower(word[index])); const auto it = ref_map.find(ch); if (it != ref_map.end()) { output[index] = ')'; output[it->second] = ')'; it->second = index; continue; } ref_map[ch] = index; } return output; } }; int main(int argc, char *argv[]) { /* "din" = > "(((" "recede" = > "()()()" "Success" = > ")())())" "(( @" = > "))((" */ TestCaseStruct test_cases[] = {{"din", "((("}, {"recede", "()()()"}, {"Success", ")())())"}, {"(( @", "))(("}}; DuplicateEncoder solution; for (const auto test_case : test_cases) { if (solution.duplicate_encoder(test_case.input) == test_case.expected_output) { cout << "test case \"" << test_case.input << "\" is pass." << std::endl; } else { cout << "test case \"" << test_case.input << "\" is fail." << std::endl; } } return EXIT_SUCCESS; } ``` ## Result PASS ###### tags: `CodeWars` `C++`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up