> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Unique Morse Code Words Python Solution

> Tested Python solution for LeetCode 804 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 804, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/unique-morse-code-words/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 804   # by problem number
lcpy gen -s unique_morse_code_words   # by problem name
```

## Problem

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:

* `'a'` maps to `".-"`,
* `'b'` maps to `"-..."`,
* `'c'` maps to `"-.-."`, and so on.

For convenience, the full table for the 26 letters of the English alphabet is given below:

```
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
```

Given an array of strings `words` where each word can be written as a concatenation of the Morse code of each letter.

* For example, `"cab"` can be written as `"-.-..--..."`, which is the concatenation of `"-.-."`, `".-"`, and `"-..."`. We will call such a concatenation the **transformation** of a word.

Return *the number of different transformations among all words we have*.

### Examples

```
Input: words = ["gin","zen","gig","msg"]
Output: 2
Explanation: The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
There are 2 different transformations: "--...-." and "--...--.".
```

```
Input: words = ["a"]
Output: 1
```

### Constraints

* 1 \<= words.length \<= 100
* 1 \<= words\[i].length \<= 12
* words\[i] consists of lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/unique_morse_code_words/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/unique_morse_code_words/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from typing import ClassVar


class Solution:
    morse: ClassVar[list[str]] = [
        ".-",
        "-...",
        "-.-.",
        "-..",
        ".",
        "..-.",
        "--.",
        "....",
        "..",
        ".---",
        "-.-",
        ".-..",
        "--",
        "-.",
        "---",
        ".--.",
        "--.-",
        ".-.",
        "...",
        "-",
        "..-",
        "...-",
        ".--",
        "-..-",
        "-.--",
        "--..",
    ]

    # Time: O(S) where S is the total number of characters in all words
    # Space: O(S) for the set of transformations
    def unique_morse_representations(self, words: list[str]) -> int:
        def transform(word: str) -> str:
            return "".join(self.morse[ord(c) - ord("a")] for c in word)

        return len({transform(w) for w in words})
```

## Complexity

| Time | Space |
| - | - |
| O(S) where S is the total number of characters in all words | O(S) for the set of transformations |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.