Welcome to Subscribe On Youtube
  • import java.util.HashSet;
    import java.util.Set;
    
    /**
    
     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:
    
     [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
     Now, given a list of words, 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 "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.
    
     Return the number of different transformations among all words we have.
    
     Example:
     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 "--...--.".
    
     */
    public class Unique_Morse_Code_Words {
        class Solution {
            public int uniqueMorseRepresentations(String[] words) {
                String[] MORSE = new String[]{
                    ".-","-...","-.-.","-..",".","..-.","--.",
                    "....","..",".---","-.-",".-..","--","-.",
                    "---",".--.","--.-",".-.","...","-","..-",
                    "...-",".--","-..-","-.--","--.."};
    
                Set<String> seen = new HashSet<>();
                for (String word: words) {
                    StringBuilder code = new StringBuilder();
                    for (char c: word.toCharArray())
                        code.append(MORSE[c - 'a']);
                    seen.add(code.toString());
                }
    
                return seen.size();
            }
        }
    }
    
    ############
    
    class Solution {
        public int uniqueMorseRepresentations(String[] words) {
            String[] codes = new String[] {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
                "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
                "..-", "...-", ".--", "-..-", "-.--", "--.."};
            Set<String> s = new HashSet<>();
            for (String word : words) {
                StringBuilder t = new StringBuilder();
                for (char c : word.toCharArray()) {
                    t.append(codes[c - 'a']);
                }
                s.add(t.toString());
            }
            return s.size();
        }
    }
    
  • // OJ: https://leetcode.com/problems/unique-morse-code-words/
    // Time: O(N)
    // Space: O(N)
    class Solution {
    private:
        string dict[26] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
    public:
        int uniqueMorseRepresentations(vector<string>& words) {
            unordered_set<string> s;
            for (string word : words) {
                string code;
                for (char c : word) code += dict[c - 'a'];
                s.insert(code);
            }
            return s.size();
        }
    };
    
  • class Solution:
        def uniqueMorseRepresentations(self, words: List[str]) -> int:
            codes = [
                ".-",
                "-...",
                "-.-.",
                "-..",
                ".",
                "..-.",
                "--.",
                "....",
                "..",
                ".---",
                "-.-",
                ".-..",
                "--",
                "-.",
                "---",
                ".--.",
                "--.-",
                ".-.",
                "...",
                "-",
                "..-",
                "...-",
                ".--",
                "-..-",
                "-.--",
                "--..",
            ]
            s = {''.join([codes[ord(c) - ord('a')] for c in word]) for word in words}
            return len(s)
    
    ############
    
    class Solution(object):
        def uniqueMorseRepresentations(self, words):
            """
            :type words: List[str]
            :rtype: int
            """
            moorse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
            trans = lambda x: moorse[ord(x) - ord('a')]
            map_word = lambda word: ''.join([trans(x) for x in word])
            res = map(map_word, words)
            return len(set(res))
    
  • func uniqueMorseRepresentations(words []string) int {
    	codes := []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
    		"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
    	s := make(map[string]bool)
    	for _, word := range words {
    		t := &strings.Builder{}
    		for _, c := range word {
    			t.WriteString(codes[c-'a'])
    		}
    		s[t.String()] = true
    	}
    	return len(s)
    }
    
  • const codes = [
        '.-',
        '-...',
        '-.-.',
        '-..',
        '.',
        '..-.',
        '--.',
        '....',
        '..',
        '.---',
        '-.-',
        '.-..',
        '--',
        '-.',
        '---',
        '.--.',
        '--.-',
        '.-.',
        '...',
        '-',
        '..-',
        '...-',
        '.--',
        '-..-',
        '-.--',
        '--..',
    ];
    
    function uniqueMorseRepresentations(words: string[]): number {
        return new Set(
            words.map(word => {
                return word
                    .split('')
                    .map(c => codes[c.charCodeAt(0) - 'a'.charCodeAt(0)])
                    .join('');
            }),
        ).size;
    }
    
    
  • use std::collections::HashSet;
    impl Solution {
        pub fn unique_morse_representations(words: Vec<String>) -> i32 {
            const codes: [&str; 26] = [
                ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
                "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
                "-.--", "--..",
            ];
            words
                .iter()
                .map(|word| {
                    word.as_bytes()
                        .iter()
                        .map(|v| codes[(v - b'a') as usize])
                        .collect::<String>()
                })
                .collect::<HashSet<String>>()
                .len() as i32
        }
    }
    
    
  • class Solution {
        public int uniqueMorseRepresentations(String[] words) {
            String[] morseArray = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
            Set<String> set = new HashSet<String>();
            for (String word : words) {
                StringBuffer sb = new StringBuffer();
                char[] array = word.toCharArray();
                for (char c : array)
                    sb.append(morseArray[c - 'a']);
                set.add(sb.toString());
            }
            return set.size();
        }
    }
    
    ############
    
    class Solution {
        public int uniqueMorseRepresentations(String[] words) {
            String[] codes = new String[] {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
                "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
                "..-", "...-", ".--", "-..-", "-.--", "--.."};
            Set<String> s = new HashSet<>();
            for (String word : words) {
                StringBuilder t = new StringBuilder();
                for (char c : word.toCharArray()) {
                    t.append(codes[c - 'a']);
                }
                s.add(t.toString());
            }
            return s.size();
        }
    }
    
  • // OJ: https://leetcode.com/problems/unique-morse-code-words/
    // Time: O(N)
    // Space: O(N)
    class Solution {
    private:
        string dict[26] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
    public:
        int uniqueMorseRepresentations(vector<string>& words) {
            unordered_set<string> s;
            for (string word : words) {
                string code;
                for (char c : word) code += dict[c - 'a'];
                s.insert(code);
            }
            return s.size();
        }
    };
    
  • class Solution:
        def uniqueMorseRepresentations(self, words: List[str]) -> int:
            codes = [
                ".-",
                "-...",
                "-.-.",
                "-..",
                ".",
                "..-.",
                "--.",
                "....",
                "..",
                ".---",
                "-.-",
                ".-..",
                "--",
                "-.",
                "---",
                ".--.",
                "--.-",
                ".-.",
                "...",
                "-",
                "..-",
                "...-",
                ".--",
                "-..-",
                "-.--",
                "--..",
            ]
            s = {''.join([codes[ord(c) - ord('a')] for c in word]) for word in words}
            return len(s)
    
    ############
    
    class Solution(object):
        def uniqueMorseRepresentations(self, words):
            """
            :type words: List[str]
            :rtype: int
            """
            moorse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
            trans = lambda x: moorse[ord(x) - ord('a')]
            map_word = lambda word: ''.join([trans(x) for x in word])
            res = map(map_word, words)
            return len(set(res))
    
  • func uniqueMorseRepresentations(words []string) int {
    	codes := []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
    		"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
    	s := make(map[string]bool)
    	for _, word := range words {
    		t := &strings.Builder{}
    		for _, c := range word {
    			t.WriteString(codes[c-'a'])
    		}
    		s[t.String()] = true
    	}
    	return len(s)
    }
    
  • const codes = [
        '.-',
        '-...',
        '-.-.',
        '-..',
        '.',
        '..-.',
        '--.',
        '....',
        '..',
        '.---',
        '-.-',
        '.-..',
        '--',
        '-.',
        '---',
        '.--.',
        '--.-',
        '.-.',
        '...',
        '-',
        '..-',
        '...-',
        '.--',
        '-..-',
        '-.--',
        '--..',
    ];
    
    function uniqueMorseRepresentations(words: string[]): number {
        return new Set(
            words.map(word => {
                return word
                    .split('')
                    .map(c => codes[c.charCodeAt(0) - 'a'.charCodeAt(0)])
                    .join('');
            }),
        ).size;
    }
    
    
  • use std::collections::HashSet;
    impl Solution {
        pub fn unique_morse_representations(words: Vec<String>) -> i32 {
            const codes: [&str; 26] = [
                ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
                "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
                "-.--", "--..",
            ];
            words
                .iter()
                .map(|word| {
                    word.as_bytes()
                        .iter()
                        .map(|v| codes[(v - b'a') as usize])
                        .collect::<String>()
                })
                .collect::<HashSet<String>>()
                .len() as i32
        }
    }
    
    

All Problems

All Solutions