unique morse code words

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:

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

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.

1
2
3
4
5
6
7
8
9
10
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 "--...--.".

method 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dots = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
words = ['gin', 'zen', 'gig', 'msg']
res ={ }
for word in words:
tem = ""
for w in word:
tem += dots[ord(w) - ord('a')]
if tem not in res:
res[tem] = 1
else:
res[tem] += 1
print(tem)
print(len(res.keys()))
print(res)

method 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
dots = {"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":"--.."}
words = ['gin', 'zen', 'gig', 'msg']
res=[]
for word in words:
stm = ""
for w in word:
stm+=dots.get(w)
print(stm)
res.append(stm)
print(res)
n=0
for i in range(len(res)):
if res[i]==res[1]:
n = n
else:
n+=1
print(n)