jeudi 15 juillet 2021

Suggestion Needed in a "Easy" problem based on Morse Code on Leetcode

Problem Link - https://leetcode.com/problems/unique-morse-code-words/

I am trying to do the mapping of all a-z characters in an unordered_map of type <string,string>

In the For loop, I am appending the corresponding value in a string which is initialised after every "word" of "words"

But I keep getting error -

Line 41: Char 33: error: no viable overloaded operator[] for type 'unordered_map<std::string, std::string>' (aka 'unordered_map<basic_string<char>, basic_string<char>>')
                str = str + (ump[words[i][j]]);
                             ~~~^~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:984:7: note: candidate function not viable: no known conversion from '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' (aka 'char') to 'const std::unordered_map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::hash<std::string>, std::equal_to<std::__cxx11::basic_string<char>>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>>>>::key_type' (aka 'const std::__cxx11::basic_string<char>') for 1st argument
      operator[](const key_type& __k)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:988:7: note: candidate function not viable: no known conversion from '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' (aka 'char') to 'std::unordered_map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>, std::hash<std::string>, std::equal_to<std::__cxx11::basic_string<char>>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>>>>::key_type' (aka 'std::__cxx11::basic_string<char>') for 1st argument
      operator[](key_type&& __k)
      ^

My Code -

     class Solution {
     public:
     int uniqueMorseRepresentations(vector<string>& words) {
        
        unordered_map<string,string> ump;
        ump["a"] = ".-";
        ump["b"] = "-...";
        ump["c"] ="-.-.";
        ump["d"] = "-..";
        ump["e"] = ".";
        ump["f"] = "..-.";
        ump["g"] = "--.";
        ump["h"] = "....";
        ump["i"] = "..";
        ump["j"] = ".---";
        ump["k"] = "-.-";
        ump["l"] = ".-..";
        ump["m"] = "--";
        ump["n"] = "-.";
        ump["o"] = "---";
        ump["p"] = ".--.";
        ump["q"] = "--.-";
        ump["r"] = ".-.";
        ump["s"] = "...";
        ump["t"] = "-";
        ump["u"] = "..-";
        ump["v"] = "...-";
        ump["w"] = ".--";
        ump["x"] = "-..-";
        ump["y"] = "-.--";
        ump["z"] = "--..";
        
        
        set<string> s;
        string str;
        for(int i=0;i<words.size();i++)
        {
            str="";
            for(int j=0;j<words[i].size();i++)
            {
                str = str + (ump[words[i][j]]);
            }
            s.push(str);
        }
        
        return s.size();
    }
};```


Aucun commentaire:

Enregistrer un commentaire