[leetcode 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:


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.

Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."

There are 2 different transformations, "--...-." and "--...--.".



  • The length of words will be at most 100.
  • Each words[i] will have length in range [1, 12].
  • words[i] will only consist of lowercase letters.

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] charToMorse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
        HashSet<String> result = new HashSet<>();
        for (String word : words) {
            String code = "";
            for (char c : word.toCharArray()) code += charToMorse[c - 'a'];
        return result.size();


Unique Words


Problem DescriptionnA common problem faced by electronic information providers is determining the number of unique words in a document. The case of a word does not affect its uniqueness. For example, The, tHE and The are all considered equivalent. Punctuation can appear in these documents and is handled as follows:n1) Periods '.' and exclamation marks '!' may appear at the end of a sentence and should not be considered a word, or part of a word.n2) Dashes '-' appear between hyphenated words. The hyphenated words should be considered separately.n3) Commas ',' colons ':' and semicolons ';' appear within a sentence and should not be considered a word, or part of a word.n4) Apostrophes ' appear within contractions and possessive forms. These symbols should be treated as if they never appeared (i.e., as if they were deleted from the word).n nnInputnThe input file contains a series of documents, each separated by an entire line of text containing only the word EOD Each document will contain no more than 1,000 lines and at most 100 unique words. All input lines will not contain more than 80 characters. Numbers, control characters, and punctuation symbols not listed above will not appear in the text. An entire line containing only the string EOT identifies the end of the list of documents; note this last document is terminated by EOT and not EODn nnOutputnThe output should be an alphabetically sorted list of all unique words, with each unique word displayed in uppercase.n nnSample InputnThe banker hammered home his two-part message! His message, nat times satirical, was that the bank's situation was a mess.nEODnHello worldnEODnThis is anfinal examplenEOTn nnSample OutputnWORDS IN DOCUMENT #1nAnATnBANKERnBANKSnHAMMEREDnHISnHOMEnMESSnMESSAGEnPARTnSATIRICALnSITATUATIONnTHATnTHEnTIMESnTWOnWASnWORDS IN DOCUMENT #2nHELLOnWORLDnWORDS IN DOCUMENT #3nAnEXAMPLEnFINALnISnTHISn