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.
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 "--...--.".
 
Note:
  • 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.


方案一:时间复杂度 O(n^2)
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String code = "[.-,-...,-.-.,-..,.,..-.,--.,....,..,.---,-.-,.-..,--,-.,---,.--.,--.-,.-.,...,-,..-,...-,.--,-..-,-.--,--..]";
Map<String, String> map = new HashMap<String, String>();
String[] codes = code.split(",");
String codeStr = null;
for(int i = 0; i < codes.length; i++) {
codeStr = codes[i];
if(i == 0) {
codeStr = codes[i].substring(1, codes[i].length());
}
if(i == codes.length - 1) {
codeStr = codes[i].substring(0, codes[i].length() -1);
}
map.put(String.valueOf((char)(i+97)), codeStr);
}
StringBuilder sb = new StringBuilder();
List<String> list = new ArrayList<String>();
for(int j = 0; j < words.length; j++) {
sb.delete(0, sb.length());
for(int k = 0; k < words[j].length(); k++) {
sb.append(map.get(String.valueOf(words[j].charAt(k))));
}
list.add(sb.toString());
}
int count = 1;
String str = null;
boolean flag = true;
for(int i = 1; i < list.size(); i++) {
flag = true;
str = list.get(i);
for(int j = i-1; j >= 0; j--) {
if(str.equals(list.get(j))) {
flag = false;
}
}
if(flag) {
count++;
}
}
return count;
}
}

方案二:

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();
    }

}

方案二转自:https://leetcode.com/problems/unique-morse-code-words/solution/#


posted @ 2018-05-22 19:47  六七十三  阅读(56)  评论(0编辑  收藏  举报