跳至主要內容

804. 唯一摩尔斯密码词


804. 唯一摩尔斯密码词

🟢   🔖  数组 哈希表 字符串  🔗 力扣open in new window LeetCodeopen in new window

题目

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 differenttransformations among all words we have.

Example 1:

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 "--...--.".

Example 2:

Input: words = ["a"]

Output: 1

Constraints:

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

题目大意

国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如:

  • 'a' 对应 ".-"
  • 'b' 对应 "-..."
  • 'c' 对应 "-.-." ,以此类推。

为了方便,所有 26 个英文字母的摩尔斯密码表如下:

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

给你一个字符串数组 words ,每个单词可以写成每个字母对应摩尔斯密码的组合。

  • 例如,"cab" 可以写成 "-.-..--..." ,(即 "-.-." + ".-" + "-..." 字符串的结合)。我们将这样一个连接过程称作 单词翻译

words 中所有单词进行单词翻译,返回不同 单词翻译 的数量。

示例 1:

输入: words = ["gin", "zen", "gig", "msg"]

输出: 2

解释:

各单词翻译如下:

"gin" -> "--...-."

"zen" -> "--...-."

"gig" -> "--...--."

"msg" -> "--...--."

共有 2 种不同翻译, "--...-." 和 "--...--.".

示例 2:

输入: words = ["a"]

输出: 1

提示:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 12
  • words[i] 由小写英文字母组成

解题思路

  1. 根据题目提供的摩尔斯密码表,将 a-z 映射到对应的摩尔斯编码。
  2. 遍历 words 数组,对于每个单词,将其每个字母翻译为摩尔斯密码,然后拼接起来。
  3. 使用 Set 数据结构存储翻译结果,这样能自动去重。
  4. 最后,返回 Set 中的元素数量,即为不同翻译的数量。

复杂度分析

  • 时间复杂度O(n)

    • 遍历单词数组 words:假设单词的总字符数为 n,每个字符需要进行摩尔斯编码转换,时间复杂度为 O(n)
    • 插入 Set 的时间复杂度为 O(1)(均摊)。
    • 总时间复杂度为 O(n)
  • 空间复杂度O(n)

    • Set 存储唯一的摩尔斯翻译,最多存储 n 个单词的翻译,空间复杂度为 O(n)
    • 存储摩尔斯密码表的数组占用 O(26) 的常数空间。
    • 总空间复杂度为 O(n)

代码

/**
 * @param {string[]} words
 * @return {number}
 */
var uniqueMorseRepresentations = function (words) {
	// prettier-ignore
	const morseCode = [
    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
    "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
    "..-", "...-", ".--", "-..-", "-.--", "--.."
  ];

	const charToMorse = (char) =>
		morseCode[char.charCodeAt(0) - 'a'.charCodeAt(0)];
	const set = new Set();

	for (const word of words) {
		let morse = '';
		for (const char of word) {
			morse += charToMorse(char);
		}
		set.add(morse);
	}

	return set.size;
};