唯一摩尔斯密码词

import java.util.HashSet;

/**
 * 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串,
 *  比如: "a" 对应 ".-", "b" 对应 "-...", "c" 对应 "-.-.", 等等。
 * 为了方便,所有26个英文字母对应摩尔斯密码表如下:
 * [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.",
 * "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
 * 给定一个单词列表,每个单词可以写成每个字母对应摩尔斯密码的组合。例如,"cab" 可以写
 * 成 "-.-..--...",(即 "-.-." + "-..." + ".-"字符串的结合)。我们将这样一个连接过程称作单词翻译。
 * 返回我们可以获得所有词不同单词翻译的数量。
 * 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, "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:
 * 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.
 * 思路:先把给的单词拼接完之后再进行比较,比较用hashset进行存取,最后返回hashset的大小即可
 * 这里用注意几个点:
 * 1、摩斯电码与26个字母对应,可以不用hashmap,直接用string[] map数组存好之后,再用
 * map[char-'a']即可。
 * 2、每个对应之后的字符串存入stringBuilder之后应该用tostring转换之后才可以放入hashSet
 * @author LemonLin
 * @Description :StringUniqueMorseCodeWords
 * @date 19.6.18-23:50
 */
public class StringUniqueMorseCodeWords {
    public int uniqueMorseRepresentations(String[] words) {
        String [] map={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.",
                "---",".--.", "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        HashSet hashSet = new HashSet();
        for (String s:words){
            StringBuilder stringBuilder= new StringBuilder();
            for (char c:s.toCharArray()){
                stringBuilder.append(map[c-'a']);
            }
            hashSet.add(stringBuilder.toString());
        }
        return hashSet.size();
    }

    public static void main(String[] args) {
        String[] words = {"gin", "zen", "gig", "msg"};
        System.out.println(new StringUniqueMorseCodeWords().uniqueMorseRepresentations(words));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
来源   中文电码,又称中文商用电码、中文电报码或中文电报明码,原本是于电报之中传送中文信息的方法。它是第一个把汉字化作电子讯号的编码表。   自摩尔电码在1835年发明后,一直只能用来传送英语或以拉丁字母拼写的文字。1873年,法国驻华人员威基杰(S·A·Viguer)参照《康熙字典》的部首排列方法,挑选了常用汉字6800多个,编成了第一部汉字电码本,名为《电报新书》。后由我国的郑观应将其改编成为《中国电报新编》。这是中国最早的汉字电码本。中国人最早研制的电报机华侨商人王承荣从法国回国后,与福州的王斌研制出我国第一台电报机,并呈请政府自办电报。清政府拒不采纳。   应用   中文电码可用作电脑里的中文输入法,但因中文电码是“无理码”,记忆困难,一般用户几乎无法熟练地掌握使用。   在香港,每个有中文姓名的市民的身份证上,均会在他的姓名下面,印有中文电码,外国人取得的入港签证亦有印上。在很多政府或商业机构的表格中,都会要求填写者填写他的中文电码,以便输入电脑。   美国签证申请表(DS-160表)中,要求申请人填写姓名的中文电码,一些生僻字没有对应的中文电码时,可用“0000”代替。   原理   中文电码表采用了四位阿拉伯数字作代号,从0001到9999按四位数顺序排列,用四位数字表示最多一万个汉字、字母和符号。汉字先按部首,后按笔划排列。字母和符号放到电码表的最尾。后来由于一万个汉字不足以应付户籍管理的要求,又有第二字面汉字的出现。在香港,两个字面都采用同一编码,由输入员人手选择字面;在台湾,第二字面的汉字会在开首补上“1”字,变成5个数字的编码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值