Skip to content

Commit 58b62d1

Browse files
committed
solved problem 804. Unique Morse Code Words
1 parent a7f1dbb commit 58b62d1

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

unique_morse_code_words/main.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,51 @@
11
package unique_morse_code_words
22

3-
type MorseVo1cabulary map[string]string
3+
type MorseVocabulary map[string]string
44

55
var morseList = []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
66

7-
var alphabetList string = "abcdefghijklmnopqrstuvwxyz"
7+
var alphabetList = "abcdefghijklmnopqrstuvwxyz"
88

9-
var vocabulary = initVocabulary(morseList, alphabetList)
9+
func initVocabulary(morse []string, alphabet string) (result MorseVocabulary) {
10+
result = make(MorseVocabulary)
1011

11-
func initVocabulary(morse []string, alphabet string) (result MorseVo1cabulary) {
12+
for i, letter := range alphabet {
13+
result[string(letter)] = morse[i]
14+
//fmt.Printf("%d: %s -> %s\n", i, string(letter), morse[i])
15+
}
1216

1317
return result
1418
}
1519

1620
func uniqueMorseRepresentations(words []string) int {
17-
18-
return 0
21+
vocabulary := make(MorseVocabulary)
22+
vocabulary = initVocabulary(morseList, alphabetList)
23+
24+
// make map of translated words
25+
var uniqueTranslations []string
26+
for _, word := range words {
27+
//fmt.Printf("Word: %s\n", word)
28+
translation := ""
29+
for _, letter := range word {
30+
//fmt.Printf(" - letter [%s] -> [ %s ]\n", string(letter), vocabulary[string(letter)])
31+
32+
translation += vocabulary[string(letter)]
33+
34+
}
35+
notSeenBefore := true
36+
// find notUnique words
37+
for _, seenTranslation := range uniqueTranslations {
38+
if translation == seenTranslation {
39+
notSeenBefore = false
40+
}
41+
}
42+
if notSeenBefore {
43+
uniqueTranslations = append(uniqueTranslations, translation)
44+
//fmt.Printf("Unique: %s\n", translated)
45+
}
46+
47+
}
48+
49+
return len(uniqueTranslations)
1950

2051
}

unique_morse_code_words/spec.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
804. Unique Morse Code Words
22

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.
3+
4+
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes,
5+
as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.
46

57
For convenience, the full table for the 26 letters of the English alphabet is given below:
68
```

unique_morse_code_words/unique_morse_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,28 @@ type TestSuite []TestCase
1717
var ts = TestSuite{
1818
TestCase{
1919
id: 1,
20-
description: "From task description - 2 uniqs",
20+
description: "From task description - 2 uniques",
2121
input: []string{"gin", "zen", "gig", "msg"},
2222
expectedResult: 2,
2323
},
24+
TestCase{
25+
id: 2,
26+
description: "one unique",
27+
input: []string{"gin", "gin", "gin", "gin", "gin", "gin"},
28+
expectedResult: 1,
29+
},
30+
TestCase{
31+
id: 3,
32+
description: "no translations",
33+
input: nil,
34+
expectedResult: 0,
35+
},
36+
TestCase{
37+
id: 4,
38+
description: "failed TestCase from leetcode - two same words in array",
39+
input: []string{"noilq", "kzlq", "ydreq", "ybxk", "kzlq"},
40+
expectedResult: 1,
41+
},
2442
}
2543

2644
func TestMorse(t *testing.T) {

0 commit comments

Comments
 (0)