Unique Morse Code Words

Description

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.

Discuss

把字母对应的字符串存起来,一次遍历每一个单词,然后去重就可以了。

Code

class Solution {
public int uniqueMorseRepresentations(String[] words) {
List<String> list = Arrays.asList(".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..",
"--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..");
Map<Integer, String> map = new HashMap<>();
int ans = 0;
for (int i = 97; i <= 122; i++) {
map.put(i, list.get(ans++));
}
Set<String> set = new HashSet<>();
for (int i = 0; i < words.length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < words[i].length(); j++) {
sb.append(map.get(Integer.valueOf(words[i].charAt(j))));
}
set.add(sb.toString());
}
return set.size();
}
}

【Leetcode】804. Unique Morse Code Words的更多相关文章

  1. 【LeetCode】804. Unique Morse Code Words 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 set + map set + 字典 日期 题目地 ...

  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】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  5. LeetCode 804. Unique Morse Code Words (唯一摩尔斯密码词)

    题目标签:String 题目给了我们 对应每一个 字母的 morse 密码,让我们从words 中 找出 有几个不同的 morse code 组合. 然后只要遍历 words,把每一个word 转换成 ...

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

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

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

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

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

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

  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. Perl 基础笔记: 使用 cpanm 安装 Perl 模块

    cpanm 其实只是一个可执行文件而已.将它下载到 bin 目录,然后添加执行权限就可以用了. $ sudo wget http://xrl.us/cpanm -O /usr/bin/cpanm; s ...

  2. 线程pthread_cleanup_push的简单例程.

    http://www.cnblogs.com/hnrainll/archive/2011/04/20/2022149.html #include<stdlib.h> #include< ...

  3. 关于Unix哲学

    http://www.ruanyifeng.com/blog/2009/06/unix_philosophy.html 这几天,我在看Unix,发现很多人在谈"Unix哲学",也就 ...

  4. redis未授权访问getshell

    redis未授权访问的问题一年前就爆了,当时刚开始学安全,还不太懂.今天借着工作的机会来搞一把,看看能不能拿下一台服务器.其实前几天就写好了一直想找个实际环境复现一下,一直没有找到,只说下大致思路. ...

  5. Ubuntu 16.04 安装 IDEA

    1.下载地址:https://www.jetbrains.com/idea/download/#section=linux 选择without jdk版本下载 2.下载完成 解压 到 /opt下 先却 ...

  6. mysql时间日期函数

    now(), current_timestamp(); -- 当前日期时间 current_date(); -- 当前日期 current_time(); -- 当前时间 date('yyyy-mm- ...

  7. Android学习笔记_22_服务Service应用之—与Activity进行相互通信的本地服务

    一.启动服务的两种方法方法: 第一种:  startService()和stopService()启动关闭服务.适用于服务和Activity之间没有调用交互的情况.如果相互之间需要方法调用或者传递参数 ...

  8. WSASocket()与Socket()的区别 转

    /**************************************************** WSASocket是Windows专用,支持异步操作:socket是unix标准,只能同步操 ...

  9. 任务学习-ucos

    1.任务(task)也称作一个线程: 2.一个任务有5种状态:休眠,就绪,运行,挂起,被中断 休眠:任务驻留在程序空间中,还没有交给ucos管理,把任务交给ucos 是通过调用OSTaskCreate ...

  10. Spring 事务声明无效果(转)

    为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异常时候 日志是打印了,但是加的事务却没有回滚. 例:      类似这样的方法不会回滚 (一个 ...