Txing

欢迎来到 | 伽蓝之堂

0%

Q804 Unique Morse Code Words

Unique Morse Code Words

Question

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, "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 1: 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 "--...--.".

Approach 1: Hash Set

Intuition and Algorithm

We can transform each word into it's Morse Code representation.

After, we put all transformations into a set seen, and return the size of the set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def uniqueMorseRepresentations(self, words):
MORSE = [".-","-...","-.-.","-..",".","..-.","--.",
"....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--.."]

seen = {"".join(MORSE[ord(c) - ord('a')] for c in word)
for word in words}

return len(seen)



def main():
Ain=["gin", "zen", "gig", "msg"]
solution=Solution().uniqueMorseRepresentations(Ain)
print(solution)


if __name__ == "__main__":
main()

Complexity Analysis

  • Time Complexity: O(S), where S is the sum of the lengths of words in words. We iterate through each character of each word in words.
  • Space Complexity: O(S).
  • Runtime: 20 ms, faster than 95.59% of Python online submissions for Unique Morse Code Words.
  • Memory Usage: 11.8 MB, less than 46.42% of Python online submissions for Unique Morse Code Words.