The Unique Morse Code Words Algorithm

  • 时间:2020-10-11 15:48:46
  • 分类:网络文摘
  • 阅读:114 次

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:

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

Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, “cba” 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.

C++ Unique Morse Code Words

We can use a unordered_set (hash set) to store the unique morse code words. We need to construct the morse code words by lookup up the morse table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        vector<string> morseTable = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        unordered_set<string> outcome;
        for (const auto &s: words) {
            string r = "";
            for (const auto &c: s) {
                r += morseTable[c - 97];
            }
            outcome.insert(r);
        }
        return outcome.size();
    }
};
class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        vector<string> morseTable = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        unordered_set<string> outcome;
        for (const auto &s: words) {
            string r = "";
            for (const auto &c: s) {
                r += morseTable[c - 97];
            }
            outcome.insert(r);
        }
        return outcome.size();
    }
};

The above C++ morse code algorithm took 8ms to complete on the leetcode online judge. The space complexity is O(N) where N is the number of strings. and the time complexity is O(NMM) where the N is the number of string and M is the average length of the string, as the string concatenation could be very time costly.

Java Unique Morse Code Words

In Java, we can speed up the string concatenation (during the process of constructing the morse code words) by using a StringBuilder – which does not copy the entire string when concatenating the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] morseTable = new String[] {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        Set<String> outcome = new HashSet<>();
        for (String s: words) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.length(); ++ i) {
                sb.append(morseTable[s.charAt(i) - 97]);
            }
            outcome.add(sb.toString());
        }
        return outcome.size();
    }
}
class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] morseTable = new String[] {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        Set<String> outcome = new HashSet<>();
        for (String s: words) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.length(); ++ i) {
                sb.append(morseTable[s.charAt(i) - 97]);
            }
            outcome.add(sb.toString());
        }
        return outcome.size();
    }
}

The above Java morse code took 4ms to complete on leetcode online judge. The space complexity is O(N) where N is the number of string and the time complexity is O(NM) where N is the number of string and M is the average length of the string.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
MAC怎么把访达的边栏项目进行排序_MAC访达边栏项目排序教程  KBS1在线直播「高清」  windows10提示“我们无法在此设备上激活windows”_windows10激活失败提示解决方法  韩国kbs2直播-韩国kbs2电视台直播「高清」  如何在墙内向Blogger独立博客上发布文章  利用Cloudflare Worker反代Blogger博客实现国内正常访问  国外免费空间1G免费php空间大流量,大家可以玩玩  韩国KBS24直播「高清」  edge浏览器怎么查看和管理扩展程序权限_edge浏览器扩展程序权限管理方法  kbs world 直播「高清」 
评论列表
添加评论