Skip to content

Commit accb7ad

Browse files
committed
feat: add solutions to lc problem: No.0804. Unique Morse Code Words
1 parent 9127064 commit accb7ad

File tree

4 files changed

+73
-17
lines changed

4 files changed

+73
-17
lines changed

solution/0800-0899/0804.Unique Morse Code Words/README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,46 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
哈希表实现。
48+
4749
<!-- tabs:start -->
4850

4951
### **Python3**
5052

5153
<!-- 这里可写当前语言的特殊实现逻辑 -->
5254

5355
```python
54-
56+
class Solution:
57+
def uniqueMorseRepresentations(self, words: List[str]) -> int:
58+
codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
59+
s = set()
60+
for word in words:
61+
t = []
62+
for c in word:
63+
t.append(codes[ord(c) - ord('a')])
64+
s.add(''.join(t))
65+
return len(s)
5566
```
5667

5768
### **Java**
5869

5970
<!-- 这里可写当前语言的特殊实现逻辑 -->
6071

6172
```java
62-
73+
class Solution {
74+
public int uniqueMorseRepresentations(String[] words) {
75+
String[] codes = new String[]{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
76+
Set<String> s = new HashSet<>();
77+
for (String word : words) {
78+
StringBuilder t = new StringBuilder();
79+
for (char c : word.toCharArray()) {
80+
t.append(codes[c - 'a']);
81+
}
82+
s.add(t.toString());
83+
}
84+
return s.size();
85+
}
86+
}
6387
```
6488

6589
### **...**

solution/0800-0899/0804.Unique Morse Code Words/README_EN.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,35 @@ There are 2 different transformations, &quot;--...-.&quot; and &quot;--...--.&qu
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def uniqueMorseRepresentations(self, words: List[str]) -> int:
50+
codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
51+
s = set()
52+
for word in words:
53+
t = []
54+
for c in word:
55+
t.append(codes[ord(c) - ord('a')])
56+
s.add(''.join(t))
57+
return len(s)
4958
```
5059

5160
### **Java**
5261

5362
```java
54-
63+
class Solution {
64+
public int uniqueMorseRepresentations(String[] words) {
65+
String[] codes = new String[]{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
66+
Set<String> s = new HashSet<>();
67+
for (String word : words) {
68+
StringBuilder t = new StringBuilder();
69+
for (char c : word.toCharArray()) {
70+
t.append(codes[c - 'a']);
71+
}
72+
s.add(t.toString());
73+
}
74+
return s.size();
75+
}
76+
}
5577
```
5678

5779
### **...**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int uniqueMorseRepresentations(String[] words) {
3+
String[] codes = new String[]{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
4+
Set<String> s = new HashSet<>();
5+
for (String word : words) {
6+
StringBuilder t = new StringBuilder();
7+
for (char c : word.toCharArray()) {
8+
t.append(codes[c - 'a']);
9+
}
10+
s.add(t.toString());
11+
}
12+
return s.size();
13+
}
14+
}
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
class Solution:
2-
def uniqueMorseRepresentations(self, words):
3-
"""
4-
:type words: List[str]
5-
:rtype: int
6-
"""
7-
8-
morse_code = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
9-
10-
unique = {}
11-
for each in words:
12-
unique["".join(morse_code[ord(ch)-97] for ch in each)] = 0
13-
14-
return len(unique)
2+
def uniqueMorseRepresentations(self, words: List[str]) -> int:
3+
codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
4+
s = set()
5+
for word in words:
6+
t = []
7+
for c in word:
8+
t.append(codes[ord(c) - ord('a')])
9+
s.add(''.join(t))
10+
return len(s)

0 commit comments

Comments
 (0)