[leetcode 804] Unique Morse Code Words

标签: leetcode
6人阅读 评论(1) 收藏 举报
分类:

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.

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] charToMorse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
        HashSet<String> result = new HashSet<>();
        for (String word : words) {
            String code = "";
            for (char c : word.toCharArray()) code += charToMorse[c - 'a'];
            result.add(code);
        }
        return result.size();
    }
}

查看评论

掌握 VSCode IDE 编辑器【专家辅导】

掌握 Visual Studio Code IDE 编辑器的使用,这一微软开发的,开源项目。 【办理终身会员】http://edu.csdn.net/lecturer/585 具体咨询 QQ 1405491181
  • 2017年02月16日 11:30

【Leetcode】804. Unique Morse Code Words

804. Unique Morse Code Wordshttps://leetcode.com/problems/unique-morse-code-words/description/Intern...
  • cheneykl
  • cheneykl
  • 2018-03-27 00:12:53
  • 108

[Leetcode] 804. Unique Morse Code Words 解题报告

题目: International Morse Code defines a standard encoding where each letter is mapped to a serie...
  • magicbean2
  • magicbean2
  • 2018-04-06 10:00:46
  • 38

LeetCode | 804. Unique Morse Code Words

题外话:时间会在指尖悄悄流逝,转眼我也毕业了。整理好行囊,明天去公司报道。打开Leetcode才发现,原来题目都出到800+了。回想当初刚知道这个平台的时候,只有300+的题目,还想计算怎么刷完全部。...
  • iLOVEJohnny
  • iLOVEJohnny
  • 2018-03-26 16:17:48
  • 189

LeetCode 804 Unique Morse Code Words

LeetCode 804 Unique Morse Code Words 传送门 题目分析 International Morse Code defines a standard ...
  • qq2667126427
  • qq2667126427
  • 2018-03-25 22:22:55
  • 96

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

题目描述:International Morse Code defines a standard encoding where each letter is mapped to a series of...
  • yuweiming70
  • yuweiming70
  • 2018-03-25 11:05:07
  • 284

leetcode 804. Unique Morse Code Words

     解题思路:HashSet不能放进重复元素。public int uniqueMorseRepresentations(String[] words) { String[] m...
  • qq_40662616
  • qq_40662616
  • 2018-04-01 11:52:35
  • 19

【leetcode练习】804. Unique Morse Code Words

International Morse Code defines a standard encoding where each letter is mapped to a series of dots...
  • miaogegegege
  • miaogegegege
  • 2018-03-25 14:49:46
  • 106

问题 C: Morse Code

问题 C: Morse Code 时间限制: 2 Sec 内存限制: 128 MB 提交: 59 解决: 27 [提交][状态][讨论版] 题目描述 摩尔斯电码(英语:Morse Code)是一...
  • hnust_V
  • hnust_V
  • 2016-06-23 20:19:46
  • 272

leetcode Weekly Contest 77 804. Unique Morse Code Words

International Morse Code defines a standard encodingwhere each letter is mapped to a series of dots ...
  • u012737193
  • u012737193
  • 2018-04-05 14:52:27
  • 13
    个人资料
    等级:
    访问量: 6万+
    积分: 1052
    排名: 4万+
    博客专栏
    最新评论