Leetcode 822 Solution

This article provides solution to leetcode question 822 (unique-morse-code-words)

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

Solution

class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        const char* codes[26];
        codes[0] = ".-";
        codes[1] = "-...";
        codes[2] = "-.-.";
        codes[3] = "-..";
        codes[4] = ".";
        codes[5] = "..-.";
        codes[6] = "--.";
        codes[7] = "....";
        codes[8] = "..";
        codes[9] = ".---";
        codes[10] = "-.-";
        codes[11] = ".-..";
        codes[12] = "--";
        codes[13] = "-.";
        codes[14] = "---";
        codes[15] = ".--.";
        codes[16] = "--.-";
        codes[17] = ".-.";
        codes[18] = "...";
        codes[19] = "-";
        codes[20] = "..-";
        codes[21] = "...-";
        codes[22] = ".--";
        codes[23] = "-..-";
        codes[24] = "-.--";
        codes[25] = "--..";

        set<string> code_set;
        for (auto& word: words)
        {
            string code_str;
            for (char ch: word)
                code_str += codes[ch - 'a'];
            code_set.insert(code_str);
        }

        return code_set.size();
    }
};