HTMLify
LeetCode - Unique Morse Code Words - Go
Views: 300 | Author: abh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | func char_to_morse(char rune) string { codes := []string{ ".-","-...","-.-.","-..",".","..-.","--.", "....","..",".---","-.-",".-..","--","-.", "---",".--.","--.-",".-.","...","-","..-", "...-",".--","-..-","-.--","--.."} return codes[char - 97] } func contain(target string, arr []string) bool { for _, v := range arr { if v == target { return true } } return false } func uniqueMorseRepresentations(words []string) int { var seen []string for _, word := range words { var morse string for _, c := range word { morse += char_to_morse(c) } if !contain(morse, seen) { seen = append(seen, morse) } } return len(seen) } |