lintcode 1013. 独特的摩尔斯编码 set

摩尔斯电码定义了一种标准编码,把每个字母映射到一系列点和短划线,例如:a -> .-,b -> -…,c ->-.-.。

给出26个字母的完整编码表格:

[".-","-…","-.-.","-…",".","…-.","–.","…","…",".—","-.-",".-…","–","-.","—",".–.","–.-",".-.","…","-","…-","…-",".–","-…-","-.–","–…"]
现在给定一个单词列表,每个单词中每个字母可以写成摩尔斯编码。 例如,cab可以写成-.-.-…-,(把c,a,b的莫尔斯编码串接起来)。 我们称之为一个词的转换。

返回所有单词中不同变换的数量。

样例1:

输入: words = ["gin", "zen", "gig", "msg"]
输出: 2
解释: 
每一个单词的变换是:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."

这里有两种不同的变换结果: "--...-."和"--...--.".
样例2:

输入: words = ["a", "b"]
输出: 2
解释: 
每一个单词的变换是:
"a" -> ".-"
"b" -> "-..."
这里有两种不同的变换结果:".-" and "-...".
class Solution {
public:
    /**
     * @param words: the given list of words
     * @return: the number of different transformations among all words we have
     */
    int uniqueMorseRepresentations(vector<string> &words) {
        // Write your code here
        string temp="";
        set<string> judge;
        vector<string> code={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        int result=0;
        int len=words.size();
        for (int i = 0; i < len; i++) {
            /* code */
            temp="";
            int slen=words[i].size();
            for(int j=0;j<slen;j++)
            {
                temp+=code[words[i][j]-'a'];
            }
            if(!judge.count(temp)) {judge.insert(temp);result++;} 
        }
        return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值