当前位置:网站首页>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;
};
边栏推荐
- MySQL basic commands
- [leetcode]75. color classification - problem solving (execution time beat 90%, memory consumption beat 78%)
- 解决Eureka默认缓存配置导致时效性问题
- Software - prerequisite software
- Qt键盘事件(一)——检测按键输入
- ZBar source code analysis - img_ scanner. c | [email protected]
- Huawei Kirin 985 mass production in the third quarter: TSMC 7Nm EUV process, integrated 5g baseband!
- Causes and solutions of QT signal and slot connection failure
- Qt设计机器人仿真控制器——按键控制机器人关节转动
- Win10 download address
猜你喜欢

多线程(基础)
![[Nanjing Agricultural University] information sharing of postgraduate entrance examination and re examination](/img/1d/550a991385b842a21e2b301725407e.png)
[Nanjing Agricultural University] information sharing of postgraduate entrance examination and re examination

MODIS data WGet Download

ArcGIS pixel size changed from 0.00025 to meters

Template method mode

【南京农业大学】考研初试复试资料分享

Envi grid resampling

How to generate complex flow chart of XMIND

Princeton calculus reader 02 Chapter 1 -- composition of functions, odd and even functions, function images

Why should we launch getaverse?
随机推荐
解决Eureka默认缓存配置导致时效性问题
Leetcode:162. looking for peak [two points looking for peak]
[Nanjing Agricultural University] information sharing of postgraduate entrance examination and re examination
Qt设计仿真机器人控制器
More than 40 Qualcomm chips have been exposed to security vulnerabilities, affecting billions of mobile phones!
Enter a URL to this page to show it. What happened in this process?
我们为什么要推出Getaverse?
JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue
ZCMU--5023: 家庭划分(C语言)
Software recommendation - office software
How to realize seamless rotation map? Write it again with native JS
QT design simulation robot controller
图片浏览器?Qt也可以实现!
EF combined with sqlbulkcopy batch insert data
文件浏览器?Qt也可以实现!
百度推广“删除重提”是什么意思?
Why should we launch getaverse?
MySQL basic commands
[leetcode]75. color classification - problem solving (execution time beat 90%, memory consumption beat 78%)
[LeetCode]巧用位运算