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/#


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伍六七AI编程

你猜你给我1分我要不要

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值