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 "--...--.". 加班之余水个题~ 利用一下set的唯一性,效率还挺高
class Solution {
public int uniqueMorseRepresentations(String[] words) {
if (words == null)
return 0;
String[] codes = new String[]{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int len = words.length;
Set<String> set = new HashSet<>();
for (int i=0; i<len; i++) {
String word = words[i];
StringBuilder codeBuilder = new StringBuilder();
for (int j=0; j<word.length(); j++) {
codeBuilder.append(codes[word.charAt(j)-'a']);
}
set.add(codeBuilder.toString());
}
return set.size();
}
}

LeetCode - 804. Unique Morse Code Words的更多相关文章

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

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

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

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

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

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

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

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

  5. LeetCode 804 Unique Morse Code Words 解题报告

    题目要求 International Morse Code defines a standard encoding where each letter is mapped to a series of ...

  6. [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table

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

  7. 804. Unique Morse Code Words - LeetCode

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

  8. 【Leetcode_easy】804. Unique Morse Code Words

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

  9. 【Leetcode】804. Unique Morse Code Words

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

随机推荐

  1. es6冲刺01

    1.let/const 1)作用域:es5中有全局作用域.函数作用域.es6中新增了块级作用域 2)let定义的变量在所在块级作用域外失效,严格模式下失效后直接报错, 且不允许重复声明同名变量 3)c ...

  2. JDBC(11)—数据库连接池

    在实际开发过程中,特别是在web应用系统中,如果程序直接访问数据库中的数据,每一次数据访问请求丢必须经历建立数据库连接.打开数据库.存取数据和关闭数据库连接.而连接并打开数据库是一件既消费资源又费时的 ...

  3. C# ManualResetEventSlim 实现

    ManualResetEventSlim通过封装 ManualResetEvent提供了自旋等待和内核等待的组合.如果需要跨进程或者跨AppDomain的同步,那么就必须使用ManualResetEv ...

  4. jsp执行过程图解

    转自:https://blog.csdn.net/y277an/article/details/76561451 一.jsp执行过程图解 用户访问jsp页面时,jsp的处理过程如下图所示: 二.预处理 ...

  5. 为RecyclerView打造通用Adapter

    ##RecycleView简单介绍 RecyclerView控件和ListView的原理有非常多相似的地方,都是维护少量的View来进行显示大量的数据.只是RecyclerView控件比ListVie ...

  6. ORA-12514 TNS:LISTENER DOES NOT CURRENTLY KNOW OF SERVICE REQUESTED IN CONNE

    对比Oracle服务器地址,端口号,还有实例名(也就是服务名).修改tnsnames.ora  在Oracle客户端的安装目录底下. 然后用sqlplus [用户名]/[密码]@[服务命名] 服务命名 ...

  7. 《图解HTTP》——返回结果的 HTTP 状态码

    状态码概述 状态码的职责是当客户端向服务器端发送请求时,描述返回的请求结果.借助状态码,用户可以知道服务器端是正常处理了请求,还是出现了错误. 状态码如 200 OK,以 3 位数字和原因短语组成.数 ...

  8. P2P贷款全攻略,贷前、贷中、贷后工作事项解析

    一.贷前调查事项 贷前调查是所有银行.小贷.P2P等等往出贷款部门的重中之重. 归根结底就是两条:让不对称信息最大限度对称.让软信息最大限度真实还原. 客户还不还款就是取决两大因素:还款能力.还款意愿 ...

  9. 单片机成长之路(51基础篇) - 002 STC单片机冷启动和复位有什么区别

    STC单片机简介 STC单片机是一款增强型51单片机,完全兼容MCS-51,还增加了新的功能,比如新增两级中断优先级,多一个外中断,内置EEPROM,硬件看门狗,具有掉电模式,512B内存等.还支持I ...

  10. 【PHP】解析PHP的GD库

    官方文档:http://php.net/manual/en/book.image.php 1.GD库简介 PHP可以创建和操作多种不同格式的图像文件.PHP提供了一些内置的图像信息函数,也可以使用GD ...