鱼C论坛

 找回密码
 立即注册
查看: 2140|回复: 0

[学习笔记] leetcode 804. Unique Morse Code Words

[复制链接]
发表于 2019-9-24 13:05:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  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.

  2. For convenience, the full table for the 26 letters of the English alphabet is given below:

  3. [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
  4. 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.

  5. Return the number of different transformations among all words we have.

  6. Example:
  7. Input: words = ["gin", "zen", "gig", "msg"]
  8. Output: 2
  9. Explanation:
  10. The transformation of each word is:
  11. "gin" -> "--...-."
  12. "zen" -> "--...-."
  13. "gig" -> "--...--."
  14. "msg" -> "--...--."

  15. There are 2 different transformations, "--...-." and "--...--.".
  16. Note:

  17. The length of words will be at most 100.
  18. Each words[i] will have length in range [1, 12].
  19. words[i] will only consist of lowercase letters.
复制代码


  1. class Solution {
  2.     public int uniqueMorseRepresentations(String[] words) {
  3.         Set <String> map = new HashSet<>();
  4.         String[] mo = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
  5.         for(String i: words){
  6.             StringBuilder a = new StringBuilder();
  7.             for(int j =0 ; j < i.length(); j++){
  8.                
  9.                 a.append(mo[i.charAt(j) - 'a']);
  10.             }
  11.             map.add(a.toString());
  12.         }
  13.         
  14.         return map.size();
  15.     }
  16. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 20:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表