|
1 | 1 | package unique_morse_code_words
|
2 | 2 |
|
3 |
| -type MorseVo1cabulary map[string]string |
| 3 | +type MorseVocabulary map[string]string |
4 | 4 |
|
5 | 5 | var morseList = []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
|
6 | 6 |
|
7 |
| -var alphabetList string = "abcdefghijklmnopqrstuvwxyz" |
| 7 | +var alphabetList = "abcdefghijklmnopqrstuvwxyz" |
8 | 8 |
|
9 |
| -var vocabulary = initVocabulary(morseList, alphabetList) |
| 9 | +func initVocabulary(morse []string, alphabet string) (result MorseVocabulary) { |
| 10 | + result = make(MorseVocabulary) |
10 | 11 |
|
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 | + } |
12 | 16 |
|
13 | 17 | return result
|
14 | 18 | }
|
15 | 19 |
|
16 | 20 | 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) |
19 | 50 |
|
20 | 51 | }
|
0 commit comments