• 804. Unique Morse Code Words


    Problem:

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.

    For convenience, the full table for the 26 letters of the English alphabet is given below:

    [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
    

    Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.

    Return the number of different transformations among all words we have.

    Example:

    Input: words = ["gin", "zen", "gig", "msg"]
    Output: 2
    Explanation: 
    The transformation of each word is:
    "gin" -> "--...-."
    "zen" -> "--...-."
    "gig" -> "--...--."
    "msg" -> "--...--."
    
    There are 2 different transformations, "--...-." and "--...--.".
    

    Note:

    The length of words will be at most 100.
    Each words[i] will have length in range [1, 12].
    words[i] will only consist of lowercase letters.
    

    思路

    Solution (C++):

    int uniqueMorseRepresentations(vector<string>& words) {
        vector<string> morse_code{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        unordered_set<string> s;
        for (auto w : words) {
            string tmp = "";
            for (auto ch : w) {
                tmp += morse_code[ch-'a'];
            }
            s.insert(tmp);
        }
        return s.size();
    }
    

    性能

    Runtime: 4 ms  Memory Usage: 7 MB

    思路

    Solution (C++):

    
    

    性能

    Runtime: ms  Memory Usage: MB

    相关链接如下:

    知乎:littledy

    欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可

    作者:littledy
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    微信公众号开发(二)用户关注
    搭建git服务器
    微信公众号开发(三)生成带参数的二维码
    windows 安装多个mysql
    微信公众号开发(一)前期 配置
    支付宝接口之条码支付
    mysql8.0 安装 修改密码 允许远程连接
    区块链开发金融交易平台
    区块链开发 在金融融资交易平台中的优势
    2019年区块链金融交易所钱包开发需要多少钱
  • 原文地址:https://www.cnblogs.com/dysjtu1995/p/12738304.html
Copyright © 2020-2023  润新知