Leetcode Note: Go - Unique Morse Code Words

Unique Morse Code Words - LeetCode
https://leetcode.com/problems/unique-morse-code-words/

  • Go 言語で取り組んだメモ

所感

  • 与えられた単語をモールス信号に変換して異なる単語数を int で return する

回答

Unique Morse Code Words - LeetCode
https://leetcode.com/problems/unique-morse-code-words/solution/

Golang solution faster than 100% - LeetCode Discuss
https://leetcode.com/problems/unique-morse-code-words/discuss/1270782/Golang-solution-faster-than-100

func uniqueMorseRepresentations(words []string) int {
	letters := []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
		".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
	m := make(map[string]int)

	for _, word := range words {
		s := ""
		for _, i := range word {
			s += letters[i - 'a']
		}
		m[s]++
	}
	return len(m)
}