zoukankan      html  css  js  c++  java
  • [LeetCode] Unique Morse Code Words

    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, "cab" 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.

    统计字符串转换成摩尔斯密码后不同的密码的个数。

    1. 将string转换成摩尔斯密码

    2. 利用unordered_map统计摩尔斯密码的个数

    3. 返回map的大小即可

    class Solution {
    public:
        int uniqueMorseRepresentations(vector<string>& words) {
            vector<string> alpha{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
            unordered_map<string, int> map;
            for (auto& word : words)
            {
                string tmp;
                for (int i = 0; i < word.size(); ++i)
                    tmp = tmp + alpha[(word[i] - 'a')];
                map[tmp]++;
            }
            return map.size();
        }
    };
  • 相关阅读:
    PHP 标量类型与返回值类型声明
    如何使用 PHP 语言来编码和解码 JSON 对象
    mongodb的读写分离
    [FWT] UOJ #310. 【UNR #2】黎明前的巧克力
    drcom 不耍流氓
    drcom 不耍流氓
    Visual Studio 自定义项目模板
    Visual Studio 自定义项目模板
    Visual Studio 自定义项目模板
    【广告】win10 uwp 水印图床 含代码
  • 原文地址:https://www.cnblogs.com/immjc/p/9148981.html
Copyright © 2011-2022 走看看