Unique Morse Code Words

Algorithm

【leetcode】Unique Morse Code Words

https://leetcode.com/problems/unique-morse-code-words/

1)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.

2)answer

将26个因为字母映射为摩斯电码,然后根据每组字母每个字符对应的摩斯电码组合起来。至于那个简写是为什么可以那么写,没搞清楚。【"cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-").】

3)solution

#include "pch.h"
#include <stdio.h>
#include <string>
#include <vector>
#include <unordered_set>
using std::string;
using std::vector;
using std::unordered_set;

class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        vector<string> morse_code = { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.." };
        // vector<string> store;
        unordered_set<string> result_val;
        int count = 0;
        for (auto str: words)
        {
            string tmp;
            for (auto ch: str)
            {
                //-'a'是找到输入字的索引。例如,'a' - 'a'为0.所以'a'对应于morse_code中的第一个元素。
                tmp += morse_code[(int)ch - (int)('a')];
            }

            result_val.insert(tmp);

        }
        return result_val.size();
    }
};

int main()
{
    vector<string> words;
    words.push_back("cba");
    // 使用内容
    Solution nSolution;
    nSolution.uniqueMorseRepresentations(words);
}

原文地址:https://www.cnblogs.com/17bdw/p/10358175.html

时间: 2024-11-08 03:26:41

Unique Morse Code Words的相关文章

LeetCode 804. Unique Morse Code Words

804. 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 &

Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题

参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: 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 &q

[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. F

[LeetCode] 804. 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. Fo

LeetCode 804 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

7. Unique Morse Code Words

Title: 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 s

morse code

morse code,摩斯电码,是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号. 摩斯电码,是一种早期的数字化通信形式,但是它不同于现代只使用0和1两种状态的二进制代码,它的代码包括5种: 点.划.点和划之间的停顿.每个字符间短的停顿(在点和划之间).每个词之间中等的停顿以及句子之间长的停顿. 下图为morse code的密码本 通过密码本,我们可以写1个测试程序 懂了这些,morse code基本入门了. 接着,说一下电报机原理 首先,电报机是由发报机与收报机

CSUOJ 1269 Morse Code Wristwatch

Description Tokyoflash推出了一款摩斯电码手表,样图如下. 要读懂这款手表,首先要了解一些摩斯电码的知识. 在了解了这些摩斯电码符号之后,就不难看懂表盘中显示的信息是“02 17 PM”了,也就是指下午2点17分.在12小时计时法中小时的取值范围为01到12之间的整数,AM代表上午,PM代表下午. 接下来我们就详细介绍一下这款摩斯电码手表是如何显示时间的. 手表的表盘是一个5*18的点阵,由6个5*3的点阵自左至右排列而成,每个5*3的点阵都显示了一个数字或字母的信息. 对于

6kyu Decode the Morse code

题目: Part of Series 1/3 This kata is part of a series on the Morse code. After you solve this kata, you may move to the next one. 系列的一部分,1/3 这个形是莫尔斯电码系列的一部分.当你解决了这个问题后,你可能会转到下一个.In this kata you have to write a simple Morse code decoder. While the Mor