LeetCode-804 Unique Morse Code Words Solution (with Java)

1. Description:

Notes:

2. Examples:

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-02-24
 3  */
 4 class Solution {
 5     public int uniqueMorseRepresentations(String[] strs) {
 6         char ch;  //current char of eace word
 7         String morse = "";  //morsecode for each word
 8         Set morseSet = new HashSet(); //used for containing morsecodes
 9         final String[] morsecode = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-",
10                                      ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...",  "-", "..-", "...-", 
11                                      ".--", "-..-", "-.--","--.." };
12         int i;
13         for (i = 0; i < strs.length; i++) {
14             for (int j = 0; j < strs[i].length(); j++) {
15                 ch = strs[i].charAt(j);
16                 morse += morsecode[ch - 'a'];
17             }
18             morseSet.add(morse);
19             morse = "";
20         }
21         return morseSet.size();
22     }
23 }

猜你喜欢

转载自www.cnblogs.com/sheepcore/p/12395976.html