• [LeetCode] 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.

    给定了26字母的摩斯电码的编码,给一组单词,把每个单词都转成摩斯码,返回有多少个不同的摩斯码。

    解法:题目很简单,直接转换判断即可。

    Java:

    public int uniqueMorseRepresentations(String[] words) {
            String[] d = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
            HashSet<String> s = new HashSet<>();
            for (String word : words) {
                String code = "";
                for (char c : word.toCharArray()) code += d[c - 'a'];
                s.add(code);
            }
            return s.size();
        }
    

    Python:

    def uniqueMorseRepresentations(self, words):
            d = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
                 "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
            return len({''.join(d[ord(i) - ord('a')] for i in w) for w in words})  

    Python:

    # Time:  O(n), n is the sume of all word lengths
    # Space: O(n)
    
    class Solution(object):
        def uniqueMorseRepresentations(self, words):
            """
            :type words: List[str]
            :rtype: int
            """
            MORSE = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
                     "....", "..", ".---", "-.-", ".-..", "--", "-.",
                     "---", ".--.", "--.-", ".-.", "...", "-", "..-",
                     "...-", ".--", "-..-", "-.--", "--.."]
    
            lookup = {"".join(MORSE[ord(c) - ord('a')] for c in word) 
                      for word in words}
            return len(lookup)  

    Python: wo

    class Solution(object):
        def uniqueMorseRepresentations(self, words):
            """
            :type words: List[str]
            :rtype: int
            """
            m = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
            trans = []
            res = 0
            for word in words:
                temp = ''
                for c in word:
                    temp += m[ord(c) - 97]
                if temp not in trans:
                    trans.append(temp)
                    res += 1
                    
            return res       

    C++:

    int uniqueMorseRepresentations(vector<string>& words) {
            vector<string> d = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
            unordered_set<string> s;
            for (auto word : words) {
                string code;
                for (auto c : word) code += d[c - 'a'];
                s.insert(code);
            }
            return s.size();
        }  

    C++:

    class Solution {
    public:
        int uniqueMorseRepresentations(vector<string>& words) {
            vector<string> morse{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
            unordered_set<string> s;
            for (string word : words) {
                string t = "";
                for (char c : word) t += morse[c - 'a'];
                s.insert(t);
            }
            return s.size();
        }
    };
    

      

    假定followup: 给一个单词的摩斯码,问有几种可能的单词,比如:"--...-.",至少有两种zen和gin

    All LeetCode Questions List 题目汇总

  • 相关阅读:
    1、Java语言概述与开发环境——Java程序运行机制
    1、Java语言概述与开发环境——JDK的安装与环境变量的配置
    针孔成像模型
    anconda下安装opencv
    用Navicat Prenium12连接Oracle数据库(oracle11g版本)时报错ORA-28547:connection to server failed,probable Oracle Net admin error.解决办法
    JQueryEsayUI的datagrid分页
    java中String和int的互相转化
    js页面刷新
    oracle的正则表达式
    EL表达式中,param和requestScope的区别
  • 原文地址:https://www.cnblogs.com/lightwindy/p/9847603.html
Copyright © 2020-2023  润新知