每日一道Leetcode算法—— Unique Morse Code Words——2019.01.17

中文:

国际莫尔斯电码定义了一种标准编码,其中每个字母映射到一系列点和短划线,如下所示:“a”映射到“.-”,“b”映射到“-...”,“c”映射到“-。-。“, 等等。

为方便起见,下面给出了英文字母26个字母的完整表格:

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

现在,给定一个单词列表,每个单词可以写成每个字母的摩尔斯代码的串联。例如,“cba”可以写成“-.- ..- ...”,(这是串联"-.-." + "-..." + ".-")。我们称之为串联,即一个词的转换。
返回我们所有单词中不同变换的数量。

注意: 单词的长度最多为100。 每个words[i]的长度范围为[1,12]。 words [i]只包含小写字母。

英文:

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.
package cn.leetcode.easy;

import java.util.HashSet;
import java.util.Set;

/**
 *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.
 * @author kimtian 
 */
public class UniqueMorseCodeWords {
    /**
     * 转码数
     *
     * @param words
     * @return
     */
    public static int uniqueMorseRepresentations(String[] words) {
        //转码数组
        String[] password = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
                ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
                "..-", "...-", ".--", "-..-", "-.--", "--.."};
        //创建一个set存放处理结果,set中会自动去除重复,最后输出set长度就是最终结果
        Set<String> set = new HashSet();
        //循环去除要转码单词的数组
        for (String word : words) {
            //使用StringBuilder就可以,不需要线程安全,会比使用String拼接效率高
            StringBuilder code = new StringBuilder();
            //由于说明words[i] 均为小写字母,所以不用toLowerCase(),如果没有说明,要转成小写,否则会出现数组越界问题
            for (char c : word.toCharArray()) {
                //使用char字段去减'a',可以获得字母所在数组的位置坐标
                code.append(password[c - 'a']);
            }
            //将StringBuilder转换为String,存放在set集合中
            set.add(code.toString());
        }
        //返回set中转换后密码的个数
        return set.size();
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        String[] words = new String[]{"gin", "zen", "gig", "msg"};
        System.out.println(uniqueMorseRepresentations(words));
    }

}

猜你喜欢

转载自blog.csdn.net/third_/article/details/86537453