【LeetCode】804. 唯一摩尔斯密码词

版权声明:made by YYT https://blog.csdn.net/qq_37621506/article/details/83310087

1.题目

国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: “a” 对应 “.-”, “b” 对应"-…", “c” 对应 “-.-.”, 等等。

为了方便,所有26个英文字母对应摩尔斯密码表如下:

[".-","-…","-.-.","-…",".","…-.","–.","…","…",".—","-.-",".-…","–","-.","—",".–.","–.-",".-.","…","-","…-","…-",".–","-…-","-.–","–…"]

给定一个单词列表,每个单词可以写成每个字母对应摩尔斯密码的组合。例如,“cab” 可以写成 “-.-.-…-”,(即 “-.-.” +"-…" + ".-"字符串的结合)。我们将这样一个连接过程称作单词翻译。返回我们可以获得所有词不同单词翻译的数量。

例如:
输入: words = [“gin”, “zen”, “gig”, “msg”]
输出: 2
解释: 各单词翻译如下:
"gin“> “–…-.”
“zen” -> “–…-.”
“gig” -> “–…--.”
“msg” -> “–…--.”
共有 2 种不同翻译, “–…-.” 和 “–…--.”.

2.思路

利用map来存储不同的字符串;
把每个字符串换成的字符串放入map中。

3.代码

string tran(string str){
    int len=str.length();
    string strMos = "";
    for(int i=0;i<len;i++){
       switch (str[i])
       {
        case 'a':
             strMos+=".-";
             break;
        case 'b':
             strMos += "-...";
             break;
        case 'c':
            strMos += "-.-.";
            break;
        case 'd':
            strMos += "-..";
            break;
        case 'e':
            strMos += ".";
            break;
        case 'f':
            strMos += "..-.";
            break;
        case 'g':
            strMos += "--.";
            break;
        case 'h':
            strMos += "....";
            break;
        case 'i':
            strMos += "..";
            break;
        case 'j':
            strMos += ".---";
            break;
        case 'k':
            strMos += "-.-";
            break;
        case 'l':
            strMos += ".-..";
            break;
        case 'm':
            strMos += "--";
            break;
        case 'n':
            strMos += "-.";
            break;
        case 'o':
            strMos += "---";
            break;
        case 'p':
            strMos += ".--.";
            break;
        case 'q':
            strMos += "--.-";
            break;
        case 'r':
            strMos += ".-.";
            break;
        case 's':
            strMos += "...";
            break;
        case 't':
            strMos += "-";
            break;
        case 'u':
            strMos += "..-";
            break;
        case 'v':
            strMos += "...-";
            break;
        case 'w':
            strMos += ".--";
            break;
        case 'x':
            strMos += "-..-";
            break;
        case 'y':
            strMos += "-.--";
            break;
        case 'z':
            strMos += "--..";
            break;
        default:
            break;
        }
    }
    return strMos;
}
int uniqueMorseRepresentations(vector<string>& words){
    map<string,int>word;
    for(int i=0;i<words.size();i++){
        word.insert(pair<string,int>(tran(words[i]),1));
    }
    cout<<word.size()<<endl;
    return word.size();
}
int main()
{
    vector<string>  words;
    words.push_back("gin");
    words.push_back("zen");
    words.push_back("gig");
    words.push_back("msg");
    vector<string>::iterator t;
    for(t=words.begin();t!=words.end();t++){
        cout<<*t<<endl;
    }
    uniqueMorseRepresentations(words);
    cout << "Hello world!" << endl;
    return 0;
}

4.参考别人代码

class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        vector<string> table{
            ".-","-...","-.-.","-..",".","..-.","--.","....",
             "..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
             "...","-","..-","...-",".--","-..-","-.--","--.."};
        
        unordered_set<string> transformations;
        
        for (auto str : words) {
            string transform;
            for (auto ch : str)
                transform.append(table[ch-'a']);
            transformations.emplace(transform);
        }
        
        return transformations.size();
    }
};

猜你喜欢

转载自blog.csdn.net/qq_37621506/article/details/83310087