804. Unique Morse Code Words

Source: Internet
Author: User

International Morse Code defines a standard encoding where all letter are mapped to a series of dots and dashes, as follow S: Maps to, maps to, "a" ".-" "b" "-..." "c" maps "-.-." to, and so on.

For convenience, the full table for the letters of the Chinese 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, the ' 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:2explanation:the transformation of each word is: "Gin"- -...-."" Zen "--...-." " Gig "--...--." " Msg ","--...--. " There is 2 different transformations, "--...-." and "--...--.".
1 //Time o (n), Space O (n) HashSet to remove duplicates2 3      Public intuniquemorserepresentations (string[] words) {4string[] Map =Newstring[]{".-", "-...", "-.-.", "-..", ".", "... -.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",".. -","...-",".--","-.. -","-.--","--.."};5 6         if(Words = =NULL|| Words.length = = 0) {7             return0;8         }9         Tenhashset<string> result =NewHashset<string>(); One          A          for(String word:words) { -StringBuilder SB =NewStringBuilder (); -              the              for(inti = 0; I < word.length (); i++) { -                  int loc = Word.charat (i)-' a ';  - sb.append (Map[loc]); -             } +              - Result.add (sb.tostring ()); +         } A          at         returnresult.size (); -}

804. Unique Morse Code Words

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.