首页 > 其他分享 >804. Unique Morse Code Words

804. Unique Morse Code Words

时间:2022-08-17 12:45:11浏览次数:57  
标签:Code .- Morse -.-. -... words each Unique

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:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Given an array of strings words where 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 of "-.-."".-", and "-...". We will call such a concatenation the transformation of a word.

Return the number of different transformations among all words we have.

 

Example 1:

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 "--...--.".

Example 2:

Input: words = ["a"]
Output: 1

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        Set<String> set = new HashSet();
        String[] dir = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        for(String s :words) {
            StringBuilder sb = new StringBuilder();
            for(char c : s.toCharArray()) sb.append(dir[c - 'a']);
            set.add(sb.toString());
        }
        return set.size();
    }
}

 

标签:Code,.-,Morse,-.-.,-...,words,each,Unique
From: https://www.cnblogs.com/wentiliangkaihua/p/16594697.html

相关文章

  • leetcode87-扰乱字符串
    扰乱字符串dpdp需要记录s1和s2的起始位置和长度,所以是一个三维dp。dp[i1][i2][len]表示s1从i1位置开始,s2从i2位置开始,长度为len的两个字符串是否和谐。分为以下几种情......
  • Codeforces1698F Equal Reversal【构造】
    分析:注意到你无论如何都无法改变a[1]的值,而你要改变a[2]的值时,你就必须要选择一个和a[1]相同的值,然后翻转这一段区间。又可以发现,任意两个数的相邻情况是不会改变的。比......
  • Codeforces Round #814 (Div. 2)
    比赛链接CodeforcesRound#814(Div.2)D2.BurenkaandTraditions(hardversion)给出\(n\)个数,每次可以选择一个区间和一个数,将该区间的所有数异或上该数,代价为区......
  • 转载_[VS Code]Visual Studio Code 添加自定义snippet(代码段),附详细配置
    [VSCode]跟我一起在VisualStudioCode添加自定义snippet(代码段),附详细配置AddcodesnippetsforCLANGinVSCode日志:2021.12.16VSCode自v1.40起,引入新的变量......
  • 【环境配置】| vscode中命令行运行如何设置默认环境、切换环境?
    1转到setting.json使用快捷键CTRL+SHIFT+P从调出的窗口中输入'python',选择解释器(Interpreter)2查看当前环境3选择目标环境4测试......
  • LeetCode 127 Word Ladder
    AtransformationsequencefromwordbeginWordtowordendWordusingadictionarywordListisasequenceofwordsbeginWord->s1->s2->...->sksuchthat:......
  • leetcode 热题100刷题-两数之和
    题题号:1题目:两数之和难度:简单链接:https://leetcode.cn/problems/two-sum/2022/08/16答案算法思路从第i个数字开始,之后的每个数字都与第i个数字相加,判断是否与目标值......
  • LeetCode 螺旋矩阵 II 算法题解 All In One
    LeetCode螺旋矩阵II算法题解AllInOnejs/ts生成螺旋矩阵螺旋矩阵原理图解动态赋值arr[i]//动态更新indexleti=0;while(left<=right&&t......
  • Visual Studio & VS Code
    前言会写这篇是因为想记入一个bug.随便以后记入一些VisualStudio和VSCode相关的冬冬呗. 当VSCodeHotReload遇上ViewComponent的Bug由于这个Bug涉......
  • leetcode85-最大矩形
    最大矩形dp+单调栈对每一层维护本列中形成的最高值height,然后对每一层分别计算最大的矩形。计算每一层最大矩形的时候,先用单调栈记录小于当前位置的左下标和右下标,矩......