Isaac Emmanuel
09 Sept 2026•EasyGo / Python / TypeScript

Unique Morse Code Words

Count distinct Morse encodings produced by a list of lowercase words.

Translate each letter through a fixed table, concatenate each word's code, and insert it into a set.

Complexity

O(total characters) time and O(w) stored encodings.

Solution files

Go•unique-morse-code-words/solution.go
package main

import "strings"

func uniqueMorseRepresentations(words []string) int {
	codes := []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
	seen := map[string]bool{}
	for _, word := range words {
		var encoded strings.Builder
		for _, letter := range word {
			encoded.WriteString(codes[letter-'a'])
		}
		seen[encoded.String()] = true
	}
	return len(seen)
}
Python•unique-morse-code-words/solution.py
class Solution:
    def uniqueMorseRepresentations(self, words: list[str]) -> int:
        codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
        return len({"".join(codes[ord(letter) - ord("a")] for letter in word) for word in words})
TypeScript•unique-morse-code-words/solution.ts
function uniqueMorseRepresentations(words: string[]): number {
  const codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."];
  return new Set(words.map((word) => [...word].map((letter) => codes[letter.charCodeAt(0) - 97]).join(""))).size;
}