Algorithm


Problem Name: 804. Unique Morse Code Words

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.

 

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.

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        vector<string>table{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        unordered_set < string>s;
        for(string x: words){
            string t = "";
            for(char c: x) t += table[c - 'a'];
            s.insert(t);
        }
        return s.size();
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
words = ["gin","zen","gig","msg"]

Output

x
+
cmd
2

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  private final String[] CODE = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
  public int uniqueMorseRepresentations(String[] words) {
    return Arrays.stream(words).map(e -> getEncoding(e)).collect(Collectors.toSet()).size();
  }
  
  private String getEncoding(String s) {
    StringBuilder sb = new StringBuilder();
    for (char c : s.toCharArray()) {
      sb.append(CODE[c - 'a']);
    }
    return sb.toString();
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
words = ["gin","zen","gig","msg"]

Output

x
+
cmd
2

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def uniqueMorseRepresentations(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        dic=set()
        letters=[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
        for word in words:
            code=[]
            for letter in word: code.append(letters[ord(letter)-ord("a")])
            code= "".join(code)
            if not code in dic: dic.add(code)
        return len(dic)
Copy The Code & Try With Live Editor

Input

x
+
cmd
words = ["a"]

Output

x
+
cmd
1

#4 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;
using System.Text;

namespace LeetCode
{
    public class _0804_UniqueMorseCodeWords
    {
        private readonly string[] MORSE_CODE = new string[] { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };

        public int UniqueMorseRepresentations(string[] words)
        {
            var map = new HashSet < string>();
            foreach (var word in words)
            {
                var sb = new StringBuilder();
                foreach (var ch in word)
                    sb.Append(MORSE_CODE[ch - 'a']);

                map.Add(sb.ToString());
            }

            return map.Count;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
words = ["a"]

Output

x
+
cmd
1
Advertisements

Demonstration


Previous
#803 Leetcode Bricks Falling When Hit Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#805 Leetcode Split Array With Same Average Solution in C, C++, Java, JavaScript, Python, C# Leetcode