前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >17 Unique Morse Code Words

17 Unique Morse Code Words

作者头像
devi
发布2021-08-18 16:00:44
2970
发布2021-08-18 16:00:44
举报
文章被收录于专栏:搬砖记录

题目

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:

代码语言:javascript
复制
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, “cba” 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:

代码语言:javascript
复制
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.

分析

题意:给定一个字符串数组,将其转换成摩斯密码,然后返回这些摩斯密码的组数(相同的摩斯密码为一组)

思路: 首先肯定是将每个字符串转换成摩斯密码,只需要遍历每个字符串,按转换规则转换即可; 然后计算不同摩斯密码的组数: 有两种方法:

  1. 初始化一个计数器,遍历所有摩斯密码对比即可,不同时将计数器+1。
  2. 将所有摩斯密码进行去重,然后计算去重后的个数(set有自动去重功能)

解答

下面答案摘自评论区大神解答,非常的巧妙。解释在注释中

代码语言:javascript
复制
class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] d = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
        //创建一个Set,用于存放最终结果,实现自动去重
        Set<String> s = new HashSet<>();
        //遍历每个字符串,假设现在是words={"gin","zen"}
        for (String w : words) {
        	//此时w="gin"
        	//创建一个临时的字符串,用于存放转码后的摩斯密码,用StringBuilder是因为它有append函数
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < w.length(); ++i)
            	//i=0, w.charAt(i)='g'
            	/*
            	这就是最巧妙的地方
				摩斯密码转换表对应的是26个大小写字母,char底层实现是int,
				利用w.charAt(i)='g'-'a',能够得到'g'在摩斯密码数组中的下标,
				通过d[w.charAt(i) - 'a'],就能拿到'g'对应的摩斯码
				*/
				//将摩斯码加入到临时字符串中
                sb.append(d[w.charAt(i) - 'a']);
            //将临时字符串加入到set集合
            s.add(sb.toString());
        }
        //最终返回集合的长度即可,重复的摩斯码已经被忽略了。
        return s.size();
    }
}

上述算法的亮点就在于,通过d[w.charAt(i) - ‘a’],实现了字母到摩斯码的映射。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/02/06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 分析
  • 解答
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档