Skip to content

Commit c433012

Browse files
author
hau
committed
Unique Morse Code Words
1 parent d15f50f commit c433012

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

unique_morse_code_words/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Unique Morse Code Words
2+
3+
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.
4+
5+
For convenience, the full table for the 26 letters of the English alphabet is given below:
6+
7+
```txt
8+
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
9+
```
10+
11+
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.
12+
13+
Return the number of different transformations among all words we have.
14+
15+
Example:
16+
17+
```txt
18+
Input: words = ["gin", "zen", "gig", "msg"]
19+
Output: 2
20+
Explanation:
21+
The transformation of each word is:
22+
"gin" -> "--...-."
23+
"zen" -> "--...-."
24+
"gig" -> "--...--."
25+
"msg" -> "--...--."
26+
27+
There are 2 different transformations, "--...-." and "--...--.".
28+
```
29+
30+
Note:
31+
32+
- The length of words will be at most 100.
33+
- Each words[i] will have length in range [1, 12].
34+
- words[i] will only consist of lowercase letters.

unique_morse_code_words/main.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("vim-go")
7+
}
8+
9+
// 26 letters of the English alphabet
10+
var morseCodes = []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
11+
12+
func uniqueMorseRepresentations_1(words []string) int {
13+
exist := make(map[string]struct{})
14+
15+
for _, word := range words {
16+
code := ""
17+
18+
for _, c := range word {
19+
code += morseCodes[c-'a']
20+
}
21+
22+
if _, ok := exist[code]; !ok {
23+
exist[code] = struct{}{}
24+
}
25+
}
26+
27+
return len(exist)
28+
}

unique_morse_code_words/main_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/haunt98/leetcode/pkg/compare"
7+
)
8+
9+
func TestUniqueMorseRepresentations_1(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
words []string
13+
want int
14+
}{
15+
{
16+
name: "example",
17+
words: []string{"gin", "zen", "gig", "msg"},
18+
want: 2,
19+
},
20+
}
21+
22+
for _, tc := range tests {
23+
t.Run(tc.name, func(t *testing.T) {
24+
got := uniqueMorseRepresentations_1(tc.words)
25+
compare.Diff(t, tc.want, got)
26+
})
27+
}
28+
}

0 commit comments

Comments
 (0)