文章目录

  • 804. 唯一摩尔斯密码词:
  • 样例 1:
  • 样例 2:
  • 提示:
  • 分析
  • 题解
    • java
    • c
    • c++
    • python
    • go
    • rust
    • typescript
  • 原题传送门:https://leetcode-cn.com/problems/unique-morse-code-words/

804. 唯一摩尔斯密码词:

国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如:

  • 'a' 对应 ".-"
  • 'b' 对应 "-..."
  • 'c' 对应 "-.-." ,以此类推。

为了方便,所有 26 个英文字母的摩尔斯密码表如下:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

给你一个字符串数组 words ,每个单词可以写成每个字母对应摩尔斯密码的组合。

  • 例如,"cab" 可以写成 "-.-..--..." ,(即 "-.-." + ".-" + "-..." 字符串的结合)。我们将这样一个连接过程称作 单词翻译

words 中所有单词进行单词翻译,返回不同 单词翻译 的数量。

样例 1:

输入: words = ["gin", "zen", "gig", "msg"]输出: 2解释: 各单词翻译如下:"gin" -> "--...-.""zen" -> "--...-.""gig" -> "--...--.""msg" -> "--...--."共有 2 种不同翻译, "--...-." 和 "--...--.".

样例 2:

输入:words = ["a"]输出:1

提示:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 12
  • words[i] 由小写英文字母组成

分析

  • 面对这道算法题目,二当家的陷入了沉思。
  • 将所有单词翻译成摩尔斯密码,看一共翻译出几种就是答案。

题解

java

class Solution {private static final String[] MORSE = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};public int uniqueMorseRepresentations(String[] words) {Set<String> seen = new HashSet<>();StringBuilder code = new StringBuilder();for (String word : words) {code.setLength(0);for (char c : word.toCharArray()) {code.append(MORSE[c - 'a']);}seen.add(code.toString());}return seen.size();}
}

c

#define MAX_STR_LEN 64typedef struct {char key[MAX_STR_LEN];UT_hash_handle hh;
} HashItem;const static char * MORSE[26] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};int uniqueMorseRepresentations(char ** words, int wordsSize){HashItem * seen = NULL;for (int i = 0; i < wordsSize; i++) {HashItem * pEntry = NULL;int len = strlen(words[i]);int pos = 0;char code[MAX_STR_LEN];for (int j = 0; j < len; j++) {pos += sprintf(code + pos, "%s", MORSE[words[i][j] - 'a']);}HASH_FIND_STR(seen, code, pEntry);if (NULL == pEntry) {pEntry = (HashItem *)malloc(sizeof(HashItem));strcpy(pEntry->key, code);HASH_ADD_STR(seen, key, pEntry);}}int ans = HASH_COUNT(seen);HashItem * curr = NULL, * tmp = NULL;HASH_ITER(hh, seen, curr, tmp) {HASH_DEL(seen, curr); free(curr);            }return ans;
}

c++

const static string MORSE[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.","....", "..", ".---", "-.-", ".-..", "--", "-.","---", ".--.", "--.-", ".-.", "...", "-", "..-","...-", ".--", "-..-", "-.--", "--.."
};class Solution {public:int uniqueMorseRepresentations(vector<string>& words) {unordered_set<string> seen;for (auto &word: words) {string code;for (auto &c: word) {code.append(MORSE[c - 'a']);}seen.emplace(code);}return seen.size();}
};

python

MORSE = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.","....", "..", ".---", "-.-", ".-..", "--", "-.","---", ".--.", "--.-", ".-.", "...", "-", "..-","...-", ".--", "-..-", "-.--", "--.."]class Solution:def uniqueMorseRepresentations(self, words: List[str]) -> int:return len(set("".join(MORSE[ord(ch) - 97] for ch in word) for word in words))

go

var morse = []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.","....", "..", ".---", "-.-", ".-..", "--", "-.","---", ".--.", "--.-", ".-.", "...", "-", "..-","...-", ".--", "-..-", "-.--", "--..",
}func uniqueMorseRepresentations(words []string) int {set := map[string]struct{}{}for _, word := range words {trans := &strings.Builder{}for _, ch := range word {trans.WriteString(morse[ch-'a'])}set[trans.String()] = struct{}{}}return len(set)
}

rust

impl Solution {pub fn unique_morse_representations(words: Vec<String>) -> i32 {let morse = vec![".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..","--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-","-.--", "--..",];let mut seen = std::collections::HashSet::new();words.iter().for_each(|word|{let mut code = String::new();word.as_bytes().iter().for_each(|c|{code.push_str(morse[(c - 97) as usize]);});seen.insert(code);});seen.len() as i32}
}

typescript

function uniqueMorseRepresentations(words: string[]): number {const morse = {"a":".-","b":"-...","c":"-.-.","d":"-..","e":".","f":"..-.","g":"--.","h":"....","i":"..","j":".---","k":"-.-","l":".-..","m":"--","n":"-.","o":"---","p":".--.","q":"--.-","r":".-.","s":"...","t":"-","u":"..-","v":"...-","w":".--","x":"-..-","y":"-.--","z":"--.."};const seen = words.map(word => {let code = '';for (let i = 0; i < word.length; i++) {code += morse[word[i]];}return code;});return new Set(seen).size;
};


原题传送门:https://leetcode-cn.com/problems/unique-morse-code-words/


非常感谢你阅读本文~
欢迎【

【算法leetcode每日一练】804. 唯一摩尔斯密码词相关推荐

  1. 《LeetCode刷题》804. 唯一摩尔斯密码词(java篇)

    题目描述: 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: 'a' 对应 ".-" , 'b' 对应 "-..." ...

  2. leetcode面试题 804. 唯一摩尔斯密码词

    leetcode面试题 804. 唯一摩尔斯密码词 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: 'a' 对应 ".-" , 'b ...

  3. java 摩尔斯电码_Java实现 LeetCode 804 唯一摩尔斯密码词 (暴力)

    804. 唯一摩尔斯密码词 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", " ...

  4. 804.唯一摩尔斯密码词

    题目 804.唯一摩尔斯密码词 题目大意 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: 'a' 对应 ".-" , 'b' 对应 ...

  5. 804. 唯一摩尔斯密码词

    804. 唯一摩尔斯密码词 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", " ...

  6. 804. 唯一摩尔斯密码词(对照转换)

    804. 唯一摩尔斯密码词 给你一个字符串数组 words ,每个单词可以写成每个字母对应摩尔斯密码的组合. 例如,"cab" 可以写成 "-.--–-" ,( ...

  7. 字符串 leetcode 804 唯一摩尔斯密码词

    题目 :唯一摩尔斯密码词 内容: 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", &q ...

  8. 【Leetcode】每日一题:唯一摩尔斯密码词

    唯一摩尔斯密码词 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: 'a' 对应 ".-" , 'b' 对应 "--&quo ...

  9. LeetCode 804. 唯一摩尔斯密码词(哈希+set)

    文章目录 1. 题目 2. 解题 1. 题目 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-&quo ...

最新文章

  1. 选择Scrum看板工具的七点特征
  2. 2021年人工智能和机器学习的五大趋势
  3. 云原生思想 — 云原生的微服务架构
  4. Intel汇编程序设计-整数算术指令(下)
  5. Android开发之限制输入框长度 | 限制EditText输入长度 | 限制AppCompatEditText长度的方法
  6. 计算机二级省份,【计算机二级】这些省份发布报名时间!调整前的最后一次考试!...
  7. php递归删除空数组,php 递归删除非空文件夹示例
  8. 【报告分享】2019年中国95后洞察报告.pdf(附下载链接)
  9. python最适合做什么-python能做什么,适不适合新手学?
  10. python xlwings api_python xlwings API接口之NumberFormat用法
  11. 【数字信号调制】基于matlab GUI QPSK调制+解调【含Matlab源码 646期】
  12. CAJ云阅读怎么用,可以修改caj文件吗?
  13. CImageList
  14. mysql pxc 原理_mysql PXC配置
  15. 苹果华为齐发力AI 人工智能手机时代来了
  16. 牛客小白月赛58 B(暴力)C(思维)D(dp滚动数组优化)
  17. 【Arduino+ESP32专题】一起来读INA3221数据手册 1
  18. 自制黑科技------桌面整理工具
  19. 微信公众号发送完红包后查看领取状态 源码
  20. 云服务器的ip地址打不开如何解决?

热门文章

  1. linux下dhcp服务器分配出去的IP地址及剩余IP地址
  2. 【C语言】猜随机数小游戏(知识点:如何产生一个随机值)
  3. 距阵乘以一个未知距阵得单位矩阵 怎么算_贷款利息怎么算,房贷车贷消费贷,利息有什么区别...
  4. CS61B | Lecture10记录
  5. c++ map unordered_map使用大全
  6. 用视频编辑软件给音频降噪的方法
  7. 【Python网络爬虫】百度贴吧/豆瓣小组
  8. 局域网SDN技术硬核内幕 二 从软件Overlay到硬件Overlay
  9. Interacting Attention Graph for Single Image Two-Hand Reconstruction(单幅图像双手重建的交互注意图)
  10. 基于ssm的校园二手物品交换系统