HTMLify

LeetCode - Unique Morse Code Words - Go
Views: 43 | Author: abh
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)
}

Comments