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);
}

Unique Morse Code Words的更多相关文章

  1. 【Leetcode】804. Unique Morse Code Words

    Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...

  2. 【Leetcode_easy】804. Unique Morse Code Words

    problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...

  3. 804. Unique Morse Code Words - LeetCode

    Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...

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

    参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...

  5. [Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  6. [LeetCode] Unique Morse Code Words 独特的摩斯码单词

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  7. (string 数组) leetcode 804. Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  8. 804. Unique Morse Code Words

    Description International Morse Code defines a standard encoding where each letter is mapped to a se ...

  9. LeetCode - 804. Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

随机推荐

  1. python各模块组合实例

    # encoding: utf-8 import requests import time from Crypto.Cipher import AES import base64 import has ...

  2. sklearn-woe/iv-乳腺癌分类器实战

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  3. nginx的下载、编译安装和启动

    一.nginx简介 nginx(“engine x”)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器.nginx是由Igor Sysoev为俄罗斯访问量第二的R ...

  4. Jquery Pagination分页插件使用

    JqueryPagination是个简单轻量级的分页插件,使用起来很容易,只要初始化一个实例,并设置总数量.翻页回调函数.其它参数就可以实现无刷新分页功能了. 准备工作 下载jquery.min.js ...

  5. mysql错误汇集

    [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated.. ...

  6. bash guide

    Table of Contents Basic Operations 1.1. File Operations 1.2. Text Operations 1.3. Directory Operatio ...

  7. 【leetcode-102,107,103】 二叉树的层次遍历

    102. 二叉树的层次遍历 (1过,隐蔽错误花时间很多,简单题目本应很快,下次注意红色错误的地方) 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: ...

  8. solr插件导入数据库中的数据

    solr插件导入数据库中的数据 1:自定义与数据库对应的域: 1.1. 设置业务系统Field 如果不使用Solr提供的Field可以针对具体的业务需要自定义一套Field. 例如:如下是商品信息Fi ...

  9. git多人协作式开发时分支管理策略

    什么是 git-flow? Git Flow是一套使用Git进行源代码管理时的一套行为规范 主分支Master 首先,代码库应该有一个.且仅有一个主分支.所有提供给用户使用的正式版本,都在这个主分支上 ...

  10. Django之名称空间

    由于name没有作用域,Django在反解URL时,会在项目全局顺序搜索,当查找到第一个name指定URL时,立即返回. project/urls.py urlpatterns = [ path('a ...