📋

Array Code

Possible Morse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// UniqueMorseRepresentations returns number of possible morse codes
func UniqueMorseRepresentations(words []string) int {
    morse := []string{".-","-...","-.-.","-..",".","..-.",
              "--.","....","..",".---","-.-",".-..","--","-.","---",
              ".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--",
              "--.."}
    tf := make(map[string]bool)
    
    for _, word := range words {
        rep := ""
        for _, r := range word {
            c := rune(r)
            i := int(c)-97 // 97 is ascii for a
            rep +=  morse[i]
        }
        tf[rep] = true
    }
    return len(tf)
}