当前位置:网站首页>804. Unique Morse code word
804. Unique Morse code word
2022-07-24 16:37:00 【My_ Bells】
Title Description
The International Morse code defines a standard encoding method , Each letter corresponds to a string of dots and dashes , such as : “a” Corresponding “.-”, “b” Corresponding “-…”, “c” Corresponding “-.-.”, wait .
For convenience , all 26 The English letters correspond to Morse code table as follows :
[".-","-…","-.-.","-…",".","…-.","–.","…","…",".—","-.-",".-…","–","-.","—",".–.","–.-",".-.","…","-","…-","…-",".–","-…-","-.–","–…"]
Given a list of words , Each word can be written as a combination of each letter corresponding to the Morse code . for example ,“cab” It can be written. “-.-…–…”,( namely “-.-.” + “-…” + ".-" A combination of strings ). We call this linking process word translation .
Back, we can get the number of different word translations of all words .
https://leetcode-cn.com/problems/unique-morse-code-words/
for example :
Input : words = [“gin”, “zen”, “gig”, “msg”]
Output : 2
explain :
The words are translated as follows :
“gin” -> “–…-.”
“zen” -> “–…-.”
“gig” -> “–…--.”
“msg” -> “–…--.”
share 2 There are two different translations , “–…-.” and “–…--.”.
Their thinking
First traversal words Array , And then I'm going to go through each one word The word gets the corresponding string in the password table according to the letters, and finally adds .
You can subtract... According to the obtained letters 97('a’ Of ASCII code ) Get the corresponding password . The last is weight removal , It can be used Set Realization .
Code implementation
// 68 ms 36 MB
/** * @param {string[]} words * @return {number} */
var uniqueMorseRepresentations = function(words) {
let arr = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
let res=new Set();
words.forEach(item=>{
let word='';
for(let k of item){
word+=arr[k.charCodeAt()-'a'.charCodeAt()];
}
res.add(word);
})
return res.size;
};
边栏推荐
- Custom view - Custom button
- Princeton calculus reader 02 Chapter 1 -- composition of functions, odd and even functions, function images
- Causes and solutions of QT signal and slot connection failure
- 简易版QQ?Qt也可以实现!(一)
- How to effectively avoid memory leakage when customizing the handler?
- 我们为什么要推出Getaverse?
- Analysis of double pointer sliding window method and solution of leetcode related problems
- JUC源码学习笔记3——AQS等待队列和CyclicBarrier,BlockingQueue
- Envi5.3 open GF-1 WFV data
- 在 PHP Web 应用程序中防止 XSS 的最佳实践
猜你喜欢

.NET 测试框架 xUnit,MSTest, Specflow 使用经验汇总

Creation and inheritance of JS class

Envi5.3 open GF-1 WFV data

我们为什么要推出Getaverse?

EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs

Why should we launch getaverse?

Summary of experience in using.Net test framework xUnit, mstest, specflow

Why should we launch getaverse?

1184. Distance between bus stops

TCP协议调试工具TcpEngine V1.3.0使用教程
随机推荐
Creation and inheritance of JS class
Jing Wei PS tutorial: basic part a
After data management, the quality is still poor
Simply use MySQL index
GEO satellite data download
What exactly is API?
Wentai technology and Baoxin software deepened 5g cooperation, and 5g manufacturing settled in Baowu, China
Qualcomm reconciled with apple and received at least $4.5 billion in settlement fees! Next goal: Huawei!
多线程(基础)
EF data migration
剑指 Offer 22. 链表中倒数第k个节点
Qt键盘事件(二)——长按按键反复触发event事件问题解决
.net review the old and know the new: [6] what is LINQ
My first blog
QT keyboard event (II) -- long press the key to trigger the event event repeatedly, and the problem is solved
What does Baidu promote "delete and recall" mean?
Solution to deepin taskbar disappearance
TCP协议调试工具TcpEngine V1.3.0使用教程
Best practices for preventing XSS in PHP web applications
简单工厂模式都不会,你真应该去工厂搬砖!