Skip to content

Commit af44459

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.8 MB (37.15%)
1 parent dae7984 commit af44459

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<p>International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:</p>
2+
3+
<ul>
4+
<li><code>&#39;a&#39;</code> maps to <code>&quot;.-&quot;</code>,</li>
5+
<li><code>&#39;b&#39;</code> maps to <code>&quot;-...&quot;</code>,</li>
6+
<li><code>&#39;c&#39;</code> maps to <code>&quot;-.-.&quot;</code>, and so on.</li>
7+
</ul>
8+
9+
<p>For convenience, the full table for the <code>26</code> letters of the English alphabet is given below:</p>
10+
11+
<pre>
12+
[&quot;.-&quot;,&quot;-...&quot;,&quot;-.-.&quot;,&quot;-..&quot;,&quot;.&quot;,&quot;..-.&quot;,&quot;--.&quot;,&quot;....&quot;,&quot;..&quot;,&quot;.---&quot;,&quot;-.-&quot;,&quot;.-..&quot;,&quot;--&quot;,&quot;-.&quot;,&quot;---&quot;,&quot;.--.&quot;,&quot;--.-&quot;,&quot;.-.&quot;,&quot;...&quot;,&quot;-&quot;,&quot;..-&quot;,&quot;...-&quot;,&quot;.--&quot;,&quot;-..-&quot;,&quot;-.--&quot;,&quot;--..&quot;]</pre>
13+
14+
<p>Given an array of strings <code>words</code> where each word can be written as a concatenation of the Morse code of each letter.</p>
15+
16+
<ul>
17+
<li>For example, <code>&quot;cab&quot;</code> can be written as <code>&quot;-.-..--...&quot;</code>, which is the concatenation of <code>&quot;-.-.&quot;</code>, <code>&quot;.-&quot;</code>, and <code>&quot;-...&quot;</code>. We will call such a concatenation the <strong>transformation</strong> of a word.</li>
18+
</ul>
19+
20+
<p>Return <em>the number of different <strong>transformations</strong> among all words we have</em>.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong class="example">Example 1:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> words = [&quot;gin&quot;,&quot;zen&quot;,&quot;gig&quot;,&quot;msg&quot;]
27+
<strong>Output:</strong> 2
28+
<strong>Explanation:</strong> The transformation of each word is:
29+
&quot;gin&quot; -&gt; &quot;--...-.&quot;
30+
&quot;zen&quot; -&gt; &quot;--...-.&quot;
31+
&quot;gig&quot; -&gt; &quot;--...--.&quot;
32+
&quot;msg&quot; -&gt; &quot;--...--.&quot;
33+
There are 2 different transformations: &quot;--...-.&quot; and &quot;--...--.&quot;.
34+
</pre>
35+
36+
<p><strong class="example">Example 2:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> words = [&quot;a&quot;]
40+
<strong>Output:</strong> 1
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= words.length &lt;= 100</code></li>
48+
<li><code>1 &lt;= words[i].length &lt;= 12</code></li>
49+
<li><code>words[i]</code> consists of lowercase English letters.</li>
50+
</ul>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def uniqueMorseRepresentations(self, words: List[str]) -> int:
3+
morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
4+
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
5+
"..-","...-",".--","-..-","-.--","--.."]
6+
# morse_set = set()
7+
# for word in words:
8+
# morse_code = ""
9+
# for char in word:
10+
# morse_code += morse[ord(char) - ord('a')]
11+
# if morse_code not in morse_set:
12+
# morse_set.add(morse_code)
13+
14+
# return len(morse_set)
15+
morse_set = {"".join([morse[ord(char) - ord('a')] for char in word]) for word in words}
16+
return len(morse_set)

0 commit comments

Comments
 (0)