804. Unique Morse Code Words

问题

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.

分析

将给定的字符串按照给定的摩斯编码拼接起来,计算给定字符串数组中所有字符串的莫斯码组合的个数.

由于不同字符串的编码可能是相同的,所以这个问题转化成为一个集合容量问题.使用Map保存编码,最后计算其容量即可.

java

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] basicCode = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        Map<String, Integer> map = new HashMap<String, Integer>();
        for(int i = 0; i < words.length; i++){
            StringBuffer temp = new StringBuffer();
            for(int j = 0; j < words[i].length(); j++){
                String code = basicCode[(int)words[i].charAt(j)-97];//根据ascii码
                temp.append(code);
            }
            map.put(temp.toString(), 0);
        }
        return map.size();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Python实现Morse Code编码和解码的示例代码: ```python MORSE_CODE_DICT = { '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': '--..', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----' } def encode(message): encoded_message = '' for letter in message.upper(): if letter == ' ': encoded_message += ' ' else: encoded_message += MORSE_CODE_DICT[letter] + ' ' return encoded_message def decode(message): message += ' ' decoded_message = '' morse_code = '' for letter in message: if letter != ' ': i = 0 morse_code += letter else: i += 1 if i == 2: decoded_message += ' ' else: decoded_message += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT.values()).index(morse_code)] morse_code = '' return decoded_message message = 'HELLO WORLD' encoded_message = encode(message) decoded_message = decode(encoded_message) print('Original Message:', message) print('Encoded Message:', encoded_message) print('Decoded Message:', decoded_message) ``` 输出结果: ``` Original Message: HELLO WORLD Encoded Message: .... . .-.. .-.. --- / .-- --- .-. .-.. -.. Decoded Message: HELLO WORLD ``` 该程序使用字典存储Morse Code编码表,encode()函数将文本消息转换为Morse Code编码,而decode()函数将Morse Code编码转换回文本消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值