0
点赞
收藏
分享

微信扫一扫

<leetcode>804.唯一摩尔斯密码词——模拟

桑二小姐 2022-04-13 阅读 23

国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如:

‘a’ 对应 “.-” ,
‘b’ 对应 “-…” ,
‘c’ 对应 “-.-.” ,以此类推。
为了方便,所有 26 个英文字母的摩尔斯密码表如下:

[“.-”,“-…”,“-.-.”,“-…”,“.”,“…-.”,“–.”,“…”,“…”,“.—”,“-.-”,“.-…”,“–”,“-.”,“—”,“.–.”,“–.-”,“.-.”,“…”,“-”,“…-”,“…-”,“.–”,“-…-”,“-.–”,“–…”]
给你一个字符串数组 words ,每个单词可以写成每个字母对应摩尔斯密码的组合。

例如,“cab” 可以写成 “-.-…–…” ,(即 “-.-.” + “.-” + “-…” 字符串的结合)。我们将这样一个连接过程称作 单词翻译 。
对 words 中所有单词进行单词翻译,返回不同 单词翻译 的数量。

解答:

const static string MORSE[] = {
            ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."
        };
class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        unordered_set<string> seen;
        for(string word : words){
            string code;
            for(char ch:word){
                code += MORSE[ch - 'a'];
            }
            seen.emplace(code);
        }
        return seen.size();
    }
};
举报

相关推荐

0 条评论